要定制Android layout 中的 attributes关键是要明白android中命名空间定义如:
xmlns:android="http://schemas.android.com/apk/res/android
以RingtonePreference为例::
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/sound_settings"
android:key="sound_settings"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
<com.android.settings.DefaultRingtonePreference
android:key="ringtone"
android:title="@string/ringtone_title"
android:summary="@string/ringtone_summary"
android:dialogTitle="@string/ringtone_title"
android:persistent="false"
android:ringtoneType="ringtone" />
在代码中::
public RingtonePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.RingtonePreference, defStyle, 0);
mRingtoneType = a.getInt(com.android.internal.R.styleable.RingtonePreference_ringtoneType,
RingtoneManager.TYPE_RINGTONE);
mShowDefault = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showDefault,
true);
mShowSilent = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showSilent,
true);
a.recycle();
}
这里注意了ringtoneType的命名空间使用的是android, 而其容器中声明了两个命名空间android, settings
::
xmlns:android="http://schemas.android.com/apk/res/android
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
何为命名空间呢?里面定义了各个类所用的属性的定义。 android这个命名空间就对应了/frameworks/base/core/res/res/values/attrs.xml文件中
定义的属性值;而settings这个命名空间就是Settings应用的res/values/attrs.xml或settings_attrs.xml文件中的属性.
如果我们查看frameworks/base/core/res/res/values/attrs.xml里面有对DefaultRingtonePreference的父类RingtonePreference的名字空间的定义:
::
<!-- Base attributes available to RingtonePreference. -->
<declare-styleable name="RingtonePreference">
<!-- Which ringtone type(s) to show in the picker. -->
<attr name="ringtoneType">
<!-- Ringtones. -->
<flag name="ringtone" value="1" />
<!-- Notification sounds. -->
<flag name="notification" value="2" />
<!-- Alarm sounds. -->
<flag name="alarm" value="4" />
<!-- All available ringtone sounds. -->
<flag name="all" value="7" />
</attr>
<!-- Whether to show an item for a default sound. -->
<attr name="showDefault" format="boolean" />
<!-- Whether to show an item for 'Silent'. -->
<attr name="showSilent" format="boolean" />
</declare-styleable>
上例中declear-styleable中的属性name对应的类名,attr则是类中的属性.
- "reference" //引用
- "color" //颜色
- "boolean" //布尔值
- "dimension" //尺寸值
- "float" //浮点值
- "integer" //整型值
- "string" //字符串
- "fraction" //百分数,比如200%
枚举型的格式:
- < attr name="orientation">
- < enum name="horizontal" value="0" />
- < enum name="vertical" value="1" />
- < /attr>
- android:orientation = "vertical"
标志位、位或运算,格式如下:
- < 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>
- android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
属性定义可以指定多种类型:
- < attr name = "background" format = "reference|color" />
android:background = "@drawable/图片ID|#00FF00"
Android 自定义属性 attrs.xml
attrs.xml设定的自定义属性,指的是在xml布局中使用的 android:text="" 等属性。
这样很多参数可以通过xml文件传入程序中。
--------------------------------------------------------------
来自:http://tech.ddvip.com/2010-07/1279183456157579.html
一、在res/values文件下定义一个attrs.xml文件.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
MyView.java
package com.android.tutor;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint mPaint;
private Context mContext;
private static final String mString = "Welcome to Mr Wei's blog";
public MyView(Context context) {
super(context);
mPaint = new Paint();
}
public MyView(Context context,AttributeSet attrs)
{
//使用自定义属性,就需要有这么一个构造函数
super(context,attrs);
mPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
//R.styleable.MyView_textColor是读取attrs中参数名,是以“样式名_参数名”的形式。
//第二个参数,是默认值,如果从xml中获取不到则使用默认值。
float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setTextSize(textSize);
mPaint.setColor(textColor);
//使用xml属性来设置代码参数。
a.recycle();
//TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性! (原文原话,不清除什么
//意思)
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
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);
}
}
}
将MyView当做一个控件加入到布局当中去。
main.xml
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
-------------------------------------------------------
注意main.xml中红色部分,它改变了显示字体的大小。