封装自定义控件

自定义属性与自定义Style

  • 利用XML中的declare-styleable标签来实现自定义属性,下面是declare-styleable标签的使用方法:
自定义控件的步骤
  • 自定义一个类MyTextView
import android.content.Context;
import android.widget.TextView;
/**
* Created with Android Studio.
* Description:
*
* @author: 王拣贤
* @date: 2019/07/05
* Time: 19:48
*/
@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {
    public MyTextView(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
    }
}
  • 新建attr.xml文件(在res/values目录下)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTextView">
        <attr name="header" format="reference"/>
        <attr name="headerHeight" format="dimension"/>
        <attr name="headerVisibleHeight" format="dimension"/>
        <attr name="age">
            <flag name = "child" value="10"/>
            <flag name = "young" value="18"/>
            <flag name = "old" value="60"/>
        </attr>
    </declare-styleable>
</resources>
  • 注意:

    • declare-styleable 中的name属性,指定了自定义控件的类名
    • 自定义属性值可以组合使用,比如
  • declare-styleable标签中涉及的标签的用法:

    • reference指的是 从string.xml、drawable.xml、color.xml等文件中引入的值
    • flag是自己定义的
    • dimension是从dimension.xml文件中引用过来的值,如果这里是dp就会进行像素转换
  • 之后在活动的布局文件中引入该自定义控件:

<com.example.adminstator.myviewdesign.AndroidHuabu.fengzhuangkongjian.MyTextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    attrstest:header="@drawable/pic1"
    attrstest:headerHeight="300dp"
    attrstest:headerVisibleHeight="100dp"
    attrstest:age="young"/>
  • 但是xml文件需要声明属性集,所以在文件头添加
xmlns:attrstest = "http://schemas.android.com/apk/res-auto"

获取自定义属性值

  • 获取属性值使用TypedArray类,这个类提供了获取某个属性值的方法,但是使用完TypedArray类之后需要使用recycle()释放资源
typedArray.getInt(int index, float defValue);
typedArray.getDimension(int index, float defValue);
typedArray.getBoolean(int index, float defValue);
typedArray.getColor(int index, float defValue);
typedArray.getString(int index);
typedArray.getDrawable(int index);
typedArray.getResources();

declare-styleable标签其他属性的用法

1.reference:参考某一资源ID

<declare-styleable name="名称">
    <attr name = "background" format = "reference"/> 对应于attrstest:background
</declare-styleable>

2.color: 颜色值

<declare-styleable>
    <attr name ="textColor" format = "color"/> 对应于attrstest:textColor
 </declare-styleable>

3.boolean:布尔值

<declare-styleable>
    <attr name = "focusable" format = "boolean"/>  对应于attrstest:focusable
 </declare-styleable>

4.dimension:尺寸值

<declare-styleable>
    <attr name = "layout_width" format = "dimension"/> 对应于attrstest:layout_width
 </declare-styleable>

5.float:属性值

<declare-styleable>
    <attr name = "fromAlpha" format = "float"/> 对应于attrstest:fromAlpha
 </declare-styleable>

6.integer:整型值

<declare-styleable>
    <attr name = "pivotX" format = "Integer"/> 对应于attrstest:pivotX
 </declare-styleable>

7.string:字符串

<declare-styleable>
    <attr name = "apiKey" format = "string"/> 对应于attrstest:apiKey
 </declare-styleable>

8.fraction:百分数

<declare-styleable>
    <attr name = "pivotX" format = "fraction"/> 对应于attrstest:pivotX
 </declare-styleable>

9.enum:枚举值

<declare-styleable>
    <attr name = "vertical" value= "0"/> 对应于attrstest:orientation="0"
 </declare-styleable>

10.flag:位或运算

<declare-styleable>
    <attr name = "adjustresize" value = "0x10"/> 
 </declare-styleable>
  • 注意: 属性的定义可以指定多种类型的值
<declare-styleable>
    <attr name = "background" format = "reference|color"/> 对应于attrstest:background="@drawable/dog|#FFFFFF"
 </declare-styleable>

猜你喜欢

转载自blog.csdn.net/qq_39424143/article/details/94765664