溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么自定義View視圖的屬性及引用

發(fā)布時間:2023-04-04 10:00:30 來源:億速云 閱讀:141 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下怎么自定義View視圖的屬性及引用的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

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

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

怎么自定義View視圖的屬性及引用

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

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

怎么自定義View視圖的屬性及引用

創(chuàng)建成功

怎么自定義View視圖的屬性及引用

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

怎么自定義View視圖的屬性及引用

注意:自定義屬性的過程及屬性和對應(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:浮點值。
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ù)組的一個潛在的二進制數(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);//給他賦值一個紅色
        setBackgroundColor(colors);

        typedArray.recycle();
    }

}

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

怎么自定義View視圖的屬性及引用

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>

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

怎么自定義View視圖的屬性及引用

附上所有的代碼 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òu)造方法,我們常用的有兩個,這里我全部添加進來
 *
 */

public class ViewDemo extends View {

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

    public ViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * TypedArray:對象描述類似數(shù)組的一個潛在的二進制數(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);//給他賦值一個紅色
        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時指定這條屬性是屬于什么類型-->
    <declare-styleable name="Myview">

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

    </declare-styleable>
</resources>

以上就是“怎么自定義View視圖的屬性及引用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI