- 自定义控件属性值类型解释
- 自定义控件属性定义及使用
- Java类中获取定义的属性内容(flag不清楚如何获取)
属性值的类型:
1、reference 引用类型值 : @id/…
2、 color 颜色类型值 #ff00ff
3、 boolean 布尔类型值 true , false
4、 dimension 尺寸类型值 dp / px /sp
5、 integer 整数类型值 weight progress max
6、float 浮点型值 0.1f
7、string 字符串类型值 “atrrs”
8、< enum > 枚举类型值 :水平/垂直
9、 flag 位或运算
10、fraction 百分数
自定义属性
一个属性名称对应一个属性值
定义属性名称
<!--自定义控件文字内容-->
<attr name="customViewText" format="string"/>
<!--自定义控件文字内容颜色-->
<attr name="customViewColor" format="color"/>
<!--自定义控件文字内容大小-->
<attr name="customViewSize" format="dimension"/>
<!--自定义控件文字前显示内容:没有;点;星号-->
<attr name="customViewPrefix">
<enum name="none" value="0"/>
<enum name="spot" value="1"/>
<enum name="askerist" value="2"/>
</attr>
<!--自定义控件背景颜色-->
<attr name="customViewBackground" format="reference"/>
<!--暂时想不到有哪些属性,瞎起了-->
<attr name="customViewInteger" format="integer"/>
<attr name="customViewFloat" format="float"/>
<attr name="customViewBoolean" format="boolean"/>
<attr name="customViewFraction" format="fraction"/>
<attr name="customViewFlag">
<flag name="flag1" value="1"/>
<flag name="flag2" value="2"/>
<flag name="flag3" value="3"/>
</attr>
<declare-styleable name="CustomView">
<attr name="customViewText"/>
<attr name="customViewColor"/>
<attr name="customViewSize"/>
<attr name="customViewPrefix"/>
<attr name="customViewBackground"/>
<attr name="customViewInteger"/>
<attr name="customViewFloat"/>
<attr name="customViewBoolean"/>
<attr name="customViewFraction"/>
<attr name="customViewFlag"/>
</declare-styleable>
使用属性
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<bin.com.customviewone.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:customViewText="CustomView"
custom:customViewColor="#cccccc"
custom:customViewSize="12sp"
custom:customViewPrefix="spot"
custom:customViewBackground="@mipmap/ic_launcher"
custom:customViewInteger="2"
custom:customViewFloat="1.0"
custom:customViewBoolean="true"
custom:customViewFraction="10%"
custom:customViewFlag="flag1|flag2"/>
</LinearLayout>
一个属性名称对应多个属性值
定义属性名称
<attr name="customViewInteger" format="integer|float"/>
使用定义的属性
custom:customViewInteger="2"
或者
custom:customViewInteger="0.2"
在java文件中获取自定义属性
在构造方法中:public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);
//reference 获取(当指向图片时转成bitmap)
int resId = typedArray.getResourceId(R.styleable.CustomView_customViewBackground, 0);
if(resId != 0)
BitmapFactory.decodeResource(getResources(), resId);
//color 获取
typedArray.getColor(R.styleable.CustomView_customViewcolor, Color.BLACK);
//boolean 获取
typedArray.getBoolean( R.styleable.CustomView_customViewBoolean, false);
//dimension 获取(都是将单位转成px)
typedArray.getDimension(R.styleable.CustomView_customViewSize, 0);//(返回float类型的值)
typedArray.getDimensionPixelSize(R.styleable.CustomView_customViewSize, 0);//(返回int,四舍五入后得到的值)
typedArray.getDimensionPixelOffset(R.styleable.CustomView_customViewSize, 0);//(返回int,去掉小数点后的数得到的值)
//integer 获取
typedArray.getInt(R.styleable.CustomView_customViewInteger, 0);
//float 获取
typedArray.getFloat(R.styleable.CustomView_customViewFloat, 0.0f);
//string 获取
typedArray.getString(R.styleable.CustomView_customViewText);
//< enum > 枚举类型获取(返回枚举对应的value值)
typedArray.getInt(R.styleable.CustomView_customViewPrefix, 0);
//flag 获取
//暂时不清楚如何获取
//fraction 获取
typedArray.getFraction(R.styleable.CustomView_customViewFraction, 4, 8, 0);
//(返回值为布局文件中设置的值乘以4或者乘以8,当R.styleable.CustomView_customViewFraction的值和15进行“&”运算,结果为0时,乘以4;结果为1时,乘以8;不以用户意志变化,所以代码中可以把4、8写成相同的值)