ids.xml
/res/values/ids.xml
ids.xml是区别于R文件的一种设置控件ID的方式。使用示例如下:
- 控件定义时
1 2 3 4
| <Button android:id = "@id/button_ok" ... />
|
- 在ids文件中添加
1 2 3 4
| <resources> <item type="id" name="button_ok">false</item> ... </resources>
|
3.在调用控件时
1 2
| Button bn = new Button(context); bn.setId(R.id.button_ok);
|
使用ids.xml的优点如下
- 命名方便,可以先将控件先命名好,在布局时直接命名
- 使用代码布局时,不需要转换
- 注意:在ids.xml中的每一项也会生成到R文件中
arrays.xml
用于包装数组
// 在arrays.xml中定义
1 2 3 4 5 6 7 8 9 10 11
| <resources> <string-array name="week"> <item>Sunday</item> <item>Monday</item> <item>Tuesday</item> <item>Wednesday</item> <item>Thursday</item> <item>Friday</item> <item>Saturday</item> </string-array> </resource>
|
在Java中调用
1
| CharSequence[] items = this.getResources().getStringArray(R.array.reboot_item);
|
attrs.xml
attrs.xml用于设定自定义属性
- 在
res/values文件夹下定义一个attrs.xml文件
1 2 3 4 5 6
| <?xml version="1.0" encoding="utf-8" ?> <resources> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> </declare-styleable> </resource>
|
- 在Java调用自定义属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class MyView extends View { private Paint mPaint; private Context mContext; public MyView(Context context) { super(context); mPaint = new Paint(); } public MyView(Context context,AttributeteSet atts){ super(context,attrs); mPaint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView); int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF); mPaint.setTextColor(textColor); a.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setStyle(Style.FILL); canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); mPaint.setColor(Color.BLUE); canvas.drawText(mString, 10, 110, mPaint); } }
|
- 布局时使用属性
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="utf-8"?> <com.android.tutor.MyView android:layout_width="fill_parent" android:layout_height="fill_parent" test:textSize="20px" test:textColor="# fff" />
|