美文网首页
Android自定义View(一)自定义属性AttributeS

Android自定义View(一)自定义属性AttributeS

作者: Wang_Mr | 来源:发表于2020-03-28 16:06 被阅读0次

自定义View的时候通常需要提供一些自定义属性,只需要在res资源目录的values目录下创建一个attrs.xml的属性定义文件,然后在该文件中定义相应的属性,通过在xml文件引用属性即可得到相应的数值。

假设自定义VIew:

public class CustomView extends FrameLayout {


    public CustomView(@NonNull Context context) {
        this(context, null);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, null, 0);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

假设在attrs.xml中自定义如下属性:

<resources>

    <!--注意 declare-styleable 的name为自定义View的类名
        在xml中使用自定义属性AS会代码提示-->
    <declare-styleable name="CustomView">
        <attr name="customAttribute" format="string" />
    </declare-styleable>

</resources>

如图所示:

代码提示功能.png

attr标签中的name表示自定义属性的名称,format表示自定义属性的类型(共11种)

  • 一、flags
    可以并存的属性值(位或运算) 例:android:configChanges="keyboardHidden|orientation|screenSize"
    1、在attrs.xml中定义属性为flags类型:
    flag标签中name代表可选择的常量,value是常量对应的值(为int类型)
<attr name="x_position" format="flags">
    <flag name="left" value="1" />
    <flag name="middle" value="10" />
    <flag name="right" value="100" />
</attr>

2、xml中使用
如果使用多个属性,用"|"分割

<!--单个使用-->
app:x_position="left"
<!--多个使用-->
app:x_position="left|right"

3、在自定义View的构造函数中获取属性的值。
获取到的int值为设置的属性值的和
比如:app:x_position="left|right"
position值为:1left对应的value+100 right对应的value=101;
如果未设置为0;
根据值的总和去判断用户设置的是什么常量。

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int position = array.getInt(R.styleable.CustomView_x_position, 0);
array.recycle();
  • 二、dimension
    尺寸类型值 例: android:paddingLeft="10dp" android:paddingRight="@dimen/dp_10"
    1、在attrs.xml中定义属性为dimension类型:
<attr name="x_text_size" format="dimension"/>

2、xml中使用

<!--直接使用尺寸数值 如 10dp  10px-->
app: x_text_size ="10dp"
app: x_text_size ="10sp"
app: x_text_size ="10px"
<!--引用dimen文件中的资源-->
app: x_text_size ="@dimen/x_20dp"

3、在自定义View的构造函数中获取属性的值。
获取到float类型的值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
float textSize = array.getDimension(R.styleable.CustomView_x_text_size, 0);
array.recycle();
  • 三、color
    颜色类型值 例: android:background="#000"
    1、在attrs.xml中定义属性为color类型:
<attr name="x_text_color" format="color"/>

2、xml中使用

<!--直接使用颜色数值 如 #fff-->
app:x_text_color="#fff"
<!--引用color文件中的资源-->
app:x_text_color="@color/colorAccent"

3、在自定义View的构造函数中获取属性的值。
获取到int类型的值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int color = array.getColor(R.styleable..CustomView_x_text_color, getResources().getColor(android.R.color.darker_gray));
array.recycle();
  • 四、string
    字符串类型值 例: android:text="java"
    1、在attrs.xml中定义属性为string类型:
<attr name="x_text" format="string"/>

2、xml中使用

<!--直接使用 如 java-->
app:x_text="Java"
<!--引用string文件中的资源-->
app:x_text="@string/app_name"

3、在自定义View的构造函数中获取属性的值。
获取到String类型的值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
String string = array.getString(R.styleable.CustomView_x_text);
array.recycle();
  • 五、reference
    引用类型值 例: android:src="@mipmap/ic_launcher"
    1、在attrs.xml中定义属性为reference类型:
<attr name="x_src" format="reference"/>

2、xml中使用

<!--直接使用资源-->
app:x_src="@mipmap/ic_launcher"
app:x_src="@array/sports"

3、在自定义View的构造函数中获取属性的值。
获取到资源的值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
int resourceId = array.getResourceId(R.styleable.CustomView.x_src, 0);
// 根据具体情况通过资源id拿到对应的value
Drawable drawable = getResources().getDrawable(resourceId);
String[] stringArray = getResources().getStringArray(resourceId);
array.recycle();
  • 六、boolean
    布尔类型值 例: android:layout_centerInParent="true"
    1、在attrs.xml中定义属性为boolean类型:
<attr name="x_center" format="boolean"/>

2、xml中使用

app: x_center ="true"

3、在自定义View的构造函数中获取属性的值。
获取到布尔值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
boolean center = array.getBoolean(R.styleable.CustomView_x_center, false);
array.recycle();
  • 七、enum
    枚举类型值 例: android:gravity="center"
    1、在attrs.xml中定义属性为enum类型:
<attr name="x_location" format="enum">
    <enum name="left" value="0" />
    <enum name="right" value="1" />
    <enum name="top" value="2" />
    <enum name="bottom" value="3" />
    <enum name="center" value="4" />
</attr>

2、xml中使用

app: x_location ="left"

3、在自定义View的构造函数中获取属性的值。
获取到int值

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
boolean center = array.getBoolean(R.styleable.CustomView_x_location, 0);
array.recycle();
  • 八、fraction
    百分比类型值
    1、在attrs.xml中定义属性为fraction类型:
<attr name="x_alpha" format="fraction" />

2、xml中使用

<!--相对于自身基准值-->
app: x_alpha ="10%"
<!--相对于父容器基准值-->
app: x_alpha ="10%p"

3、在自定义View的构造函数中获取属性的值。
获取到float值(10% 自身基准值【1】 为0.1 ,10%p 父容器基准值【2】 为0.2)

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
// 第二个参数为自身基准值,第三个参数为父容器基准值
float alpha = array.getFraction(R.styleable.CustomView_x_alpha, 1, 2, 1);
array.recycle();
  • 十一、混合类型
    属性定义时可以指定多种类型的值 用"|"分开
    比如需要设置背景既可以是颜色或者是一张图片
    1、在attrs.xml中定义属性为color|reference类型:
<attr name="x_background" format="color|reference"/>

2、xml中使用

<!--引用图片资源-->
app:x_background="@drawable/serach_bg"
<!--直接使用color-->
app:x_background="#fff"

3、在自定义View的构造函数中获取属性的值。
获取到drawable

TypedArray array = getContext().obtainStyledAttributes(attrs,R.styleable.CustomView);
Drawable drawable = array.getDrawable(R.styleable.CustomView_x_background);
array.recycle();

相关文章

网友评论

      本文标题:Android自定义View(一)自定义属性AttributeS

      本文链接:https://www.haomeiwen.com/subject/ujtsuhtx.html