android 自定义控件自定义属性

我们在自定义控件的时候必不可少要去自定义一些属性,使这些自定义的属性能够在xml中赋值然后供我们使用。

如何去自定义属性?
1,定义attrs.xml文件
2,在布局文件中标注、赋值
3,在自定义控件中获取、使用
4,测试

定义attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
    reference     引用
    color            颜色
    boolean       布尔值
    dimension   尺寸值
    float            浮点值
    integer        整型值
    string          字符串
    enum          枚举值
    declare-styleable 标签只能设置一次
  -->

    <declare-styleable name="app">
        <attr name="bl" format="boolean" />
        <attr name="str" format="string" />
        <attr name="ref" format="reference" />
        <attr name="inte" format="integer" />
        <attr name="dim" format="dimension" />
        <!-- enum声明的属性,value只能是int型-->
        <attr name="relation">
            <enum name="icon_left" value="0" />
            <enum name="icon_right" value="1" />
            <enum name="icon_above" value="2" />
            <enum name="icon_below" value="3" />
        </attr>
    </declare-styleable>
</resources>

自定义一个布局

public class TestLayout extends LinearLayout {
    public TestLayout(Context context) {
        super(context);
    }

    public TestLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TestLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
}

在布局文件中使用自定义属性

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--
        xmlns:testRes="http://schemas.android.com/apk/res/com.lh.test"
        说明:
        xmlns      是XML name space 的缩写;
        testRes   随便写,最好与attrs.xml中declare-styleable name="testRes"的名字一致
        http://schemas.android.com/apk/res/    此为android固定格式
        com.lh.knowledge    此应用的包名,如manifest配置文件中一致。
        -->
        <com.lh.test.layout.TestLayout xmlns:app="http://schemas.android.com/apk/res/com.lh.knowledge"
            android:id="@+id/test_layout"
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:relation = "icon_left"
            app:bl="false"
            app:ref="@color/colorAccent"
            app:str="aaaaaaaaaaaa" />

    </LinearLayout>

在布局中使用自定义属性

public class TestLayout extends LinearLayout {
    public TestLayout(Context context) {
        super(context);
    }

    public TestLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获得attrs.xml中的属性对象
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.app);
        //获得相对应的属性值
        String str = a.getString(R.styleable.app_str);
        boolean bl = a.getBoolean(R.styleable.app_bl, true);
        int relationInt = a.getInt(R.styleable.app_relation, 0);
        int color = a.getResourceId(R.styleable.app_ref, R.color.colorPrimaryDark);
        //将属性值打印出来
        Log.e("test", "bl:" + bl);
        Log.e("test", "str:" + str);
        Log.e("test", "relationInt:" + relationInt);
        //使用获得的属性
        setBackgroundResource(color);
        //回收资源对象
        a.recycle();

    }

    public TestLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }
}

自定义控件中自定义属性的用法就是这样,非常简单

发布了113 篇原创文章 · 获赞 48 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/yehui928186846/article/details/78398087