欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

圖文詳解自定義View視圖的屬性及引用

 更新時(shí)間:2023年04月04日 08:49:50   作者:小白的成長之路  
這篇文章主要介紹了圖文詳解自定義View視圖的屬性及引用,由于Android自帶的視圖無法滿足自己需求,又或者美觀度不夠自己的要求,我們就要自來親自設(shè)計(jì)自己的視圖,需要的朋友可以參考下

本章講解:自定義視圖,我們需要做哪些準(zhǔn)備!

對于一些中級的開發(fā)者來說就要接觸到自定義視圖,由于Android自帶的視圖無法滿足自己需求,又或者美觀度不夠自己的要求,我們就要自來親自設(shè)計(jì)自己的視圖。那么如何來實(shí)現(xiàn)自定義視圖呢?下面我們先簡單的來認(rèn)識下如何實(shí)現(xiàn)自定義視圖!

第一步 自定義視圖首先需要什么?我們都要做那些簡單的準(zhǔn)備?

1、我們需要?jiǎng)?chuàng)建一個(gè)類,來繼承View

2、我們需要自己去實(shí)現(xiàn)自定義視圖需求的各種資源屬性

3、引用我們定義好的自定義屬性

4、我們還經(jīng)常用到3個(gè)方法onMeasure(),onLayout(),onDraw(),(這里先不講)

一、創(chuàng)建一個(gè)類,繼承View

會提示我們添加構(gòu)造方法,它擁有4個(gè),我們起碼要用2個(gè),如下

這里寫圖片描述

二、如何創(chuàng)建自定義屬性呢?

2-1:創(chuàng)建一個(gè)資源文件

這里寫圖片描述

創(chuàng)建成功

這里寫圖片描述

2-2:打開我們創(chuàng)建好的資源文件,來寫我們需要的屬性,我簡單的寫了兩個(gè),如圖:

這里寫圖片描述

注意:自定義屬性的過程及屬性和對應(yīng)的類別

>自定義屬性:
1. reference:參考某一資源ID,以此類推
(1)屬性定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>

(2)屬性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>

2. color:顏色值
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>

3. boolean:布爾值
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>

4. dimension:尺寸值。注意,這里如果是dp那就會做像素轉(zhuǎn)換
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>

5. float:浮點(diǎn)值。
6. integer:整型值。
7. string:字符串
8. fraction:百分?jǐn)?shù)。
9. enum:枚舉值
10. flag:是自己定義的,類似于 android:gravity="top",就是里面對應(yīng)了自己的屬性值。
11. reference|color:顏色的資源文件。
12.reference|boolean:布爾值的資源文件

三、如何引用我們的自定義的資源

可以通過TypedArray 類來接受我們自定義的屬性,也可以在xml中來指定我們自定義的屬性

3-1:在代碼中引用

public class ViewDemo extends View {

    public ViewDemo(Context context) {
        super(context);
    }

    public ViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * TypedArray:對象描述類似數(shù)組的一個(gè)潛在的二進(jìn)制數(shù)據(jù)的緩沖區(qū)(官方描述)
         * 就是系統(tǒng)在默認(rèn)的資源文件R.styleable中去獲取相關(guān)的配置。
         * 如果appearance不為空,它就會去尋找獲取相關(guān)屬性
         * 也就是沖我們自定屬性樣式中,來引用你需要的某條屬性
        */
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
        int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個(gè)紅色
        setBackgroundColor(colors);

        typedArray.recycle();
    }

}

引用后設(shè)置為顏色為紅色,我們布局中的組件就會變成紅色

這里寫圖片描述

3-2:引用我們的資源?

我們還可以在我們的布局中直接引用我們的屬性,如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:attrs_ViewDemo="http://schemas.android.com/apk/res/tester.ermu.com.viewtext"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ResAuto">

    <tester.ermu.com.viewtext.ViewDemo
        android:layout_width="match_parent"
        android:layout_height="50dp"
        attrs_ViewDemo:rect_color = "#ff00ff00"/>

</LinearLayout>

運(yùn)行效果,已經(jīng)覆蓋紅色

這里寫圖片描述

附上所有的代碼 MainActivity

package tester.ermu.com.viewtext;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}

ViewDemo

package tester.ermu.com.viewtext;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by ENZ on 2016/11/17.
 * 1、我們讓ViewDemo繼承View
 * 2、他有4個(gè)構(gòu)造方法,我們常用的有兩個(gè),這里我全部添加進(jìn)來
 *
 */

public class ViewDemo extends View {

    public ViewDemo(Context context) {
        super(context);
    }

    public ViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * TypedArray:對象描述類似數(shù)組的一個(gè)潛在的二進(jìn)制數(shù)據(jù)的緩沖區(qū)(官方描述)
         * 就是系統(tǒng)在默認(rèn)的資源文件R.styleable中去獲取相關(guān)的配置。
         * 如果appearance不為空,它就會去尋找獲取相關(guān)屬性
         * 也就是沖我們自定屬性樣式中,來引用你需要的某條屬性
        */
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.Myview);
        int colors =typedArray.getColor(R.styleable.Myview_rect_color,0xffff0000);//給他賦值一個(gè)紅色
        setBackgroundColor(colors);

        typedArray.recycle();
    }

}

main布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="tester.ermu.com.viewtext.MainActivity">

    <include
       layout="@layout/activity_viewdemo" />
</RelativeLayout>

自定義view布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:attrs_ViewDemo="http://schemas.android.com/apk/res/tester.ermu.com.viewtext"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="ResAuto">

    <tester.ermu.com.viewtext.ViewDemo
        android:layout_width="match_parent"
        android:layout_height="50dp"
        attrs_ViewDemo:rect_color = "#ff00ff00"/>

</LinearLayout>

自定義屬性資源

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--我們自定義的屬性,名字為Myview,attr是其中屬性的意思,format時(shí)指定這條屬性是屬于什么類型-->
    <declare-styleable name="Myview">

        <attr name="rect_color" format="color"></attr>
        <attr name="rect_text" format="reference"></attr>

    </declare-styleable>
</resources>

到此這篇關(guān)于圖文詳解自定義View視圖的屬性及引用的文章就介紹到這了,更多相關(guān)自定義View視圖屬性引用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論