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

自定义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中自定义如下属性:



    
    
        
    


如图所示:

Android自定义View(一)自定义属性AttributeSet_第1张图片
代码提示功能.png

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

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

    
    
    

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类型:

2、xml中使用


app: x_text_size ="10dp"
app: x_text_size ="10sp"
app: x_text_size ="10px"

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类型:

2、xml中使用


app:x_text_color="#fff"

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类型:

2、xml中使用


app:x_text="Java"

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类型:

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类型:

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类型:

    
    
    
    
    

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类型:

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类型:

2、xml中使用


app:x_background="@drawable/serach_bg"

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(一)自定义属性AttributeSet)