[Android 自定义 View] 自定义属性你真的理解吗?

Android 高手核心知识点笔记(不断更新中🔥)点击查看 PS:各位童鞋不要忘记给我 star 一波哦~~
在这里插入图片描述
我的 Android 知识体系,欢迎 Star https://github.com/daishengda2018/AndroidKnowledgeSystem

自定义属性其实就是一些 xml 标签,他们通过 xml 文件的形式,可以配置某些 View 的信息,让自定义 View 使用起来更加灵活。

想必很多同学都已经对于自定义属性使用的得心应手了,但是有一些细节你真的知道吗?比如 AttributeSet、TypedArray 、declare-styleable 这些类和标签的内容你都清楚吗,在获取自定义属性的时候为什么要用

Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);

方法呢?所有的答案都会在这篇文章里。

必须是 res/values/attrs.xml吗?

很多文章都说:需要在 res/values 目录下创建 attrs.xml 文件然后在里面写我们需要的属性,其实这是不太准确的,通过实验证明,文件的名字可以随意指定,不一定必须是 attrs.xml !

[外链图片转存失败(img-MF8eTbMt-1565511612610)(assets/image-20190809010946000.png)]

例如笔者自定义了一个 custom.xml 文件, 里面的内容符合自定义属性的规范,在 View 中也是可以正常访问到的。(具体原因尚不清楚,可能是 Android Stuido 的功能)

文件结构

文件结构

  1. name space : 命名空间,名字可以随便起,但是最好和自定义 View 的名字相同,因为 Android Stuido 可以帮我们做一些事情,比如说 command + 鼠标左键,可以直接跳转

  2. attr name :这就是我们自定义属性的名字,具体的格式还是模仿 android 内部的方式,驼峰式命名或者是 名称_名称

  3. format : 属性的具体类型,此处讲解一些特殊的类型,此处不是重点,网上文章很多。

    a .reference: 资源id

    <ImageView android:background = "@drawable/图片ID"/>
    

    b. fraction : 百分数

    • 属性定义
    <attr name="pivotX" format="fraction"/>
    
    • 使用
    <android:pivotX = "200%"/>
    

    c. flag : 位运算,可以在使用过程中指定多个值

    • 定义
    <attr name="gravity" format="flags">
    	<flag name="top" value="0x30"/>
    	<flag name="bottom" value="0x50" />
    	<flag name="left" value="0x03" />
    	<flag name="right" value="0x05" />
    	<flag name="center_vertical" value="0x10" />
    </attr>
    
    • 使用
    <TextView android:gravity="bottom|left"/>
    

    d. enum : 枚举

    • 属性定义
    <attr name="orientation" format="enum">
    	<enum name="horizontal" value="0"/>
    	<enum name="vertical" value="1"/>
    </attr>
    

    e. 混合模式 :指定属性的时候可以指定多种类型值

     <attr name="background" format="reference|color"/>
    

属性的使用

  1. 定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomAttrsDemo">
        <attr name="text_color" format="color" />
        <attr name="text" format="dimension" />
    </declare-styleable>

</resources>
  1. 在 xml 文件中使用

    在布局文件中使用,首先需要引入命名空间,这样才能找到我们包中的 attrs,这里我们引入了命名空间 app,res-auto 表示自动查找

  xmlns:app="http://schemas.android.com/apk/res-auto"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.dsd.demo.ui.draw.attrs.CustomAttrsDemo
        android:id="@+id/custom_attrs_demo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        app:text_color="#333333"
        app:text_size="10sp"/>

</FrameLayout>
  1. 在自定义 View 中使用
    此处要注意的是 TypedArray 在使用后一定要调用 TypedArray.recycler() 方法进行回收,否则会内存泄漏。
   /**
    * 自定义属性 Demo
    *
    * Created by im_dsd on 2019-08-11
    */
   public class CustomAttrsDemo extends android.support.v7.widget.AppCompatTextView {
   
       private final int mTextColor;
       private final int mTextSize;
   
       public CustomAttrsDemo(Context context, AttributeSet attrs) {
           super(context, attrs);
           TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomAttrsDemo);
           mTextColor = array.getColor(R.styleable.CustomAttrsDemo_textColor, Color.BLACK);
           mTextSize = array.getDimensionPixelSize(R.styleable.CustomAttrsDemo_textSize, 18);
           // 注意使用完成之后一定要回收
           array.recycle();
       }
   }

AttributeSet、TypedArray 、declare-styleable

AttributeSet

A collection of attributes, as found associated with a tag in an XML document. Often you will not want to use this interface directly, instead passing it to {@link android.content.res.Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int) Resources.Theme.obtainStyledAttributes()}

可以看到 AtttirbuteSet 是一个大的属性集合,装载了此 View 所有的属性,用户可以通过方法:

 Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);

获取指定的属性集合(一个明确的小集合 TypedArray)

TypedArray

TypedArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.XXXX);

TypedArray里面装的就是具体的属性了,我们可以通过 :array.getXXXX 的方法获取具体的属性值

注意: 在使用后一定要调用array.recycle 用于释放内存空间,不然此内存空间就被浪费了

declare-styleable

此标签的作用就是将属性分组,在 Context.obtainStyledAttributes 方法中指定需要加载的属性组

这次自定义属性就完成了。

总结

自定义属性还是很简单的,但是很多同学都把加载自定义属性的过程当错了模版代码背了下来,不明白的其中的道理,在使用过程中还是很难达到灵活运用。

  1. 总的来说,自定义属性就是为了让自定义 View 在 xml 文件中更加灵活,让更多的属性可以被使用者控制。
  2. 自定义的时候我们需要在 res/values 文件夹下创建 attrs.xml 文件(文件名称不是固定的,可以随意起但是还是叫 attrs.xml 统一使用习惯的较好)然后在文件中使用 declare-styleable 标签自定义一个属性组。
  3. 在 layout.xml 文件中使用的时候需要引入命名空间
  4. 在具体的自定义 View 使用中,需要使用方法
 TypeArray array = Context.obtainStyledAttributes(AttributeSet, R.styleable.属性集合名);

获取指定的属性集 R.styleable.属性集合名,然后通过方法:

array.getXXXX(R.styleable.属性集合名_属性名, 默认值)的方式获取属性
array.recyler()

方法获取属性值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值