一.前言
attr翻译过来的意思是属性,在android中简单来说就是我们可以为控件来自定义属性,一般attrs.xml
都是和自定义View来配合进行使用的,所以下面会来写一个简单的自定义View来举例说明attrs.xml
的使用。
二.使用方法
1.创建attrs.xml
在values目录下新建attrs.xml
,然后在里面进行自定义属性的添加:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="theme">
<attr name="rect_color" format="color"/>
</declare-styleable>
</resources>
我们在配置文件中定义了名为theme的自定义属性组合,在里面我们定义了name
为rect_color
的属性,它的值的格式为color
。
2.创建自定义View
接下来就是创建一个简单的矩形View并将我们的自定义属性解析,代码如下:
public class RectView extends View {
private Paint paint;
private int color = Color.RED;
public RectView(Context context) {
super(context);
initDraw();
}
public RectView(Context context,AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.theme); //获得attrs中名为theme的属性组
color = typedArray.getColor(R.styleable.theme_rect_color,Color.RED); //将颜色设置为theme里面属性名为theme_rect的值,如果没有设定,则默认值为red
typedArray.recycle(); //将资源进行释放
initDraw();
}
public RectView(Context context,AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initDraw();
}
private void initDraw(){
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setStrokeWidth((float) 1.5);
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
canvas.drawRect(0,0,width,height,paint);
}
}
3.在布局文件里面进行引用
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yzbkaka.attrstest.MainActivity">
<com.example.yzbkaka.attrstest.RectView
android:layout_width="200dp"
android:layout_height="200dp"
app:rect_color="@android:color/holo_green_dark"/>
</android.support.constraint.ConstraintLayout>
属性生效
属性生效
默认属性
三.attrs.xml里面的format介绍
format就是指定我们自定义属性所需要设定的格式,常用的格式如下:
- reference:参考某一资源ID
<declare-styleable name = "theme">
<attr name = "my_background" format = "reference" />
</declare-styleable>
<MyView
android:layout_width = "42dip"
android:layout_height = "42dip"
app:my_background = "@drawable/图片ID"/>
- color:颜色值
<declare-styleable name = "theme">
<attr name = "my_color" format = "color" />
</declare-styleable>
<MyView
android:layout_width = "42dip"
android:layout_height = "42dip"
app:textColor = "#00FF00"/>
- dimension:尺寸值
<declare-styleable name = "名称">
<attr name = "my_width" format = "dimension" />
</declare-styleable>
<MyView
android:id="@+id/myView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:my_width="20dp"/>
- string:字符串
<declare-styleable name = "Theme">
<attr name = "my_Text" format = "string" />
</declare-styleable>
<MyView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
app:my_text = "yzbkaka" />
- float:浮点值
<declare-styleable name = "Theme">
<attr name = "my_fromAlpha" format = "float" />
<attr name = "my_toAlpha" format = "float" />
</declare-styleable>
<MyView
app:fromAlpha = "1.0"
app:toAlpha = "0.7"/>