理解values—attr文件

attr全称attribute,意思是属性,性质的意思(个人理解),Android中的attr文件用于描述自定义view的自定义属性

安卓自带的attr属性有常见的比如view的layout_width,id,text都是在attr里描述好的属性。

安卓sdk中attr描述(部分示例)

 <declare-styleable name="TextView">
        <!-- Determines the minimum type that getText() will return.
             The default is "normal".
             Note that EditText and LogTextBox always return Editable,
             even if you specify something less powerful here. -->
        <attr name="bufferType">
            <!-- Can return any CharSequence, possibly a
             Spanned one if the source text was Spanned. -->
            <enum name="normal" value="0" />
            <!-- Can only return Spannable. -->
            <enum name="spannable" value="1" />
            <!-- Can only return Spannable and Editable. -->
            <enum name="editable" value="2" />
        </attr>
        <!-- Text to display. -->
        <attr name="text" format="string" localization="suggested" />
        <!-- Hint text to display when the text is empty. -->
        <attr name="hint" format="string" />
        <!-- Text color. -->
        <attr name="textColor" />
 </declare-styleable>

看过原生的定义了,下面介绍我们如何自定义attr

 1   <declare-styleable name="MyView">
         2  <attr  name ="属性1" format = "这个属性的取值格式">
            3  <enum name="取值1" value="程序中对应的值"/>
               <enum name="取值1" value="程序中对应的值"/>
               <enum name="取值1" value="程序中对应的值"/>
               <enum name="取值1" value="程序中对应的值"/>
            4  <flag name="取值1" value="程序中对应的值" />
               <flag name="取值2" value="程序中对应的值" />
               <flag name="取值3" value="程序中对应的值" />
    </declare-styleable>

声明一个MyView的属性组,属性组下面有很多属性attr,可以用enum,和flag为attr提供默认值选项(可以不填)。

下面介绍一个重要的东西,attr的格式——format:
format的选项
  • fraction:百分比
  • reference: 资源引用
    剩下的看字面意思以此类推了。
如果想引用attr的值赋值给别的属性:
            android:textColor="?attr/qmui_config_color_gray_5"
            android:textSize="?attr/qmui_common_list_item_detail_h_text_size"

注意attr值在此之前一定被赋值过,才能赋值给别人。

attr值解析

被解析attr示例:

<declare-styleable name="roundedimageview">
        <attr name="border_thickness" format="" />
        <attr name="border_inside_color" format="color" />
        <attr name="border_outside_color" format="color"/>
        <attr name="border_radius" format="dimension"/>
    </declare-styleable>

解析模板

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.属性组名称, defStyleAttr, defStyleRes);
typedArray .getXX(R.styleable.XX_xxx);
typedArray .recycle();

解析示例

        //   这个attr参数是构造方法传来的
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
        leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
        leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
        rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
        rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
        coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
        typedArray.recycle();

obtainStyledAttributes()用来获得一个TypedArray(一个存放attr属性的数组)


obtainStyledAttributes()
  • set:一个和xml中的标签关联的存放属性的集合
  • attrs:我们要在xml中读取的属性
  • defStyleAttr:当前主题中的一个属性,其中包含对为TypedArray提供默认值的样式资源的引用。可以为0以不寻找默认值。
  • defStyleRes:是具体的style资源。为TypedArray提供默认值的样式资源的资源标识符,仅当defStyleAttr为0或在主题中找不到时才使用。 可以为0以不寻找默认值。

其中set和defStyle可以使用构造传来的参数,attr使用我们自定义的attr属性。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容