版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chennai1101/article/details/82983703
相关文章
Android 自定义控件属性
Android 自定义控件属性format详解
Android 自定义控件属性赋值
前言
在自定义属性时,需要在declare-styable节点下添加attr,名称以name定义,类型以format定义。
1. format取值
- reference,引用值
- color,颜色值
- boolean,布尔值
- dimension,尺寸值
- float,浮点值
- integer,整型值
- string,字符串
- fraction,百分数值
- enum,枚举
- flag,位
2. 自定义属性
(1) attrs.xml文件
自定义AttrFormatTextView
,使用所有类型的属性。
<declare-styleable name="AttrFormatTextView">
<attr name="attrReference" format="reference" />
<attr name="attrColor" format="color" />
<attr name="attrBoolean" format="boolean" />
<attr name="attrDimension" format="dimension" />
<attr name="attrFloat" format="float" />
<attr name="attrInteger" format="integer" />
<attr name="attrString" format="string" />
<attr name="attrFraction" format="fraction" />
<attr name="attrEnum" >
<enum name="east" value="1" />
<enum name="west" value="2" />
<enum name="south" value="3" />
<enum name="north" value="4" />
</attr>
<attr name="attrFlag">
<flag name="top" value="0x01" />
<flag name="bottom" value="0x02" />
<flag name="left" value="0x10" />
<flag name="right" value="0x20" />
</attr>
</declare-styleable>
(2) 自定义控件AttrFormatTextView
public class AttrFormatTextView extends TextView {
public AttrFormatTextView(Context context) {
this(context, null);
}
public AttrFormatTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AttrFormatTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AttrFormatTextView);
int refValue = a.getResourceId(R.styleable.AttrFormatTextView_attrReference, 0);
int colorValue = a.getColor(R.styleable.AttrFormatTextView_attrColor, 0);
boolean boolValue = a.getBoolean(R.styleable.AttrFormatTextView_attrBoolean, false);
int dimenValue = a.getDimensionPixelSize(R.styleable.AttrFormatTextView_attrDimension, 0);
float floatValue = a.getFloat(R.styleable.AttrFormatTextView_attrFloat, 0);
int intValue = a.getInteger(R.styleable.AttrFormatTextView_attrInteger, 0);
String strValue = a.getString(R.styleable.AttrFormatTextView_attrString);
float fractionValue = a.getFraction(R.styleable.AttrFormatTextView_attrFraction, 100, 200, 0);
int enumValue = a.getInt(R.styleable.AttrFormatTextView_attrEnum, 0);
int flagValue = a.getInt(R.styleable.AttrFormatTextView_attrFlag, 0);
a.recycle();
if (colorValue != 0) {
setTextColor(colorValue);
}
String text = (refValue != 0 ? "ref = " + getResources().getString(refValue) + "\n" : "")
+ (boolValue ? "bool = true\n" : "")
+ (dimenValue != 0 ? "dimen = " + dimenValue + "\n" : "")
+ (floatValue != 0 ? "float = " + floatValue + "\n" : "")
+ (intValue != 0 ? "integer = " + intValue + "\n" : "")
+ (strValue != null ? "string = " + strValue + "\n" : "")
+ (fractionValue != 0 ? "fraction = " + fractionValue + "\n" : "")
+ (enumValue != 0 ? "enum = " + enumValue + "\n" : "")
+ (flagValue != 0 ? "flag = " + getFlagValue(flagValue) + "\n" : "");
setText(text);
}
private String getFlagValue(int flag) {
String flagValue = "";
if ((flag & 0x01) != 0) {
flagValue += "Top";
}
if ((flag & 0x02) != 0) {
flagValue += flagValue.length() == 0 ? "" : " | ";
flagValue += "Bottom";
}
if ((flag & 0x10) != 0) {
flagValue += flagValue.length() == 0 ? "" : " | ";
flagValue += "Left";
}
if ((flag & 0x20) != 0) {
flagValue += flagValue.length() == 0 ? "" : " | ";
flagValue += "Right";
}
return flagValue;
}
}
(3) 布局文件
在布局文件中,分别使用各种类型数据
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.blog.demo.custom.view.AttrFormatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:attrReference="@string/app_name"
app:attrColor="#ffff0000"
app:attrBoolean="true"
app:attrDimension="15dp"
app:attrFloat="1.43"
app:attrInteger="36"
app:attrString="This is a String"
app:attrFraction="50%"
app:attrEnum="south"
app:attrFlag="top|left"/>
</LinearLayout>
(4) 效果如下
3. fraction类型
fraction百分数值有两种方式,分别是50%
和50%p
。
在方法getFraction(int index, int base, int pbase, float defValue)中,如果值是50%,使用base0.5。一旦定义为50%p,则使用pbase0.5。
<com.blog.demo.custom.view.AttrFormatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:attrFraction="50%p" />
效果如下
4. enum和flag类型
这两个类型都使用int值来计算,区别在于enum只能选择某个值,而flag可以选择多个值。
<com.blog.demo.custom.view.AttrFormatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:attrEnum="east"
app:attrFlag="bottom|right"/>
效果如下