1.使用xml文件可以快速有效的定义我们app的界面,可是有时候官方定义的一些基本组件不够用,于是我们就需要自定义组件来满足我们的开发需求,因此,我们就借助attrs.xml来实现,改目录位于res/values目录:下面是studio创建attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--是一个declare-styleable组,这个组名字myStudy(想写什么写什么最好顾名思义),后面布局文件中会用到, attr标签是定义属性名字以及属性值 --> <declare-styleable name="myStudy"> <attr name="textColor" format="color"></attr> <attr name="textSize" format="dimension"></attr> </declare-styleable> <!--是一个declare-styleable组,也可以不要,直接写attr属性 --> </resources>
2.简单写一下自定义的myView:
public class MyView extends View { private Paint mPaint; private static final String mString = "Study Custom Attrs !!!!"; public MyView(Context context, int[] myStudy) { super(context); } public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); //TypedArray是一个数组容器:作用就是资源的映射作用 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myStudy); //设置TextView的各个属性值 float textSize = ta.getDimension(R.styleable.myStudy_textSize , 30); int textColor = ta.getColor(R.styleable.myStudy_textColor , 0xFFFFFFF); //为画笔设置各个属性值 mPaint.setTextSize(textSize); mPaint.setColor(textColor); //释放内存回收TypedArray, ta.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(Color.RED); //设置填充 // Paint.Style.FILL :填充内部 // Paint.Style.FILL_AND_STROKE :填充内部和描边 // Paint.Style.STROKE :仅描边 mPaint.setStyle(Paint.Style.FILL); //绘制矩形控件:左、上、右 、底 canvas.drawRect(new Rect(10 , 10 ,310 , 100) ,mPaint); //画笔颜色 mPaint.setColor(Color.BLUE); //绘制文字:坐标X10 ,y100 canvas.drawText(mString, 20, 65 ,mPaint); }
3.布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:myStudy="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.wangqing.studyattrs.MainActivity"> <com.example.wangqing.studyattrs.MyView android:id="@+id/id_my" android:layout_width="wrap_content" android:layout_height="wrap_content" myStudy:textSize="14sp" myStudy:textColor="#ff22ff" /> </RelativeLayout>
布局文件xmlns:myStudy="http://schemas.android.com/apk/res-auto"就是对我们自定义的attrs引入,
myStudy:textSize="14sp" myStudy:textColor="#ff22ff"属性就是我们attrs定义的属性
4.进入知识拓展:
001.两个styleable,同时包含了相同的属性custom,这时在编译时会提示Attribute “xxx” has already been defined,表示相同属性重复定义:a.相同styleable name不能再同一个attr.xml中重复定义,b.styleable name不一致attir name也不能重复定义,attr format属性不影响重复定义结果。因此可以采用如下方法解决该问题
<declare-styleable name="Sample">
<attr name="custom" format="string|reference" />
</declare-styleable>
<declare-styleable name="Sample1">
<attr name="custom" format="string|reference" />
</declare-styleable>
<attr name="custom" format="string|reference" />
<declare-styleable name="Sample">
<attr name="custom" />
</declare-styleable>
<declare-styleable name="Sample1">
<attr name="custom" />
</declare-styleable>
002.AttributeSet与TypedArray
构造方法中的有个参数叫做AttributeSet(eg: MyView(Context context, AttributeSet attrs) )这个参数看名字就知道包含的是参数的集合,那么我能不能通过它去获取我的自定义属性呢?
首先AttributeSet中的确保存的是该View声明的所有的属性,并且外面的确可以通过它去获取(自定义的)属性,怎么做呢?
其实看下AttributeSet的方法就明白了,下面看代码。
public MyView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test); String text = ta.getString(R.styleable.test_testAttr); int textAttr = ta.getInteger(R.styleable.test_text, -1); Log.e(TAG, "text = " + text + " , textAttr = " + textAttr); ta.recycle(); }
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int count = attrs.getAttributeCount();
for (int i = 0; i < count; i++) {
String attrName = attrs.getAttributeName(i);
String attrVal = attrs.getAttributeValue(i);
Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);
}
// ==>use typedarray ...
}
但是,在
android:layout_width="@dimen/dp100"
android:layout_height="@dimen/dp200"
这种情况时AttributeSet取得值就是一个不知道的东西,而TypedArray依然可以取得对应的值,所以TypedArray是用来简化我们的工作的,再次建议大家使用TypedArray
003.Android自定义属性详解(网络粘贴):
1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)属性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮点值。
(1)属性定义:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)属性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>
7. string:字符串。
(1)属性定义:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>
8. fraction:百分数。
(1)属性定义:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<rotate xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:枚举值。
(1)属性定义:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)属性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. flag:位或运算。
(1)属性定义:
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>
(2)属性使用:
<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"
/>