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

2020-03-28  本文已影响0人  Wang_Mr

自定义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种)

<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();
<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();
<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();
<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();
<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();
<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();
<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();
<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();
<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();
上一篇 下一篇

猜你喜欢

热点阅读