要定制Android layout 中的 attributes關(guān)鍵是要明白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, 而其容器中聲明了兩個(gè)命名空間android, settings
::
xmlns:android="http://schemas.android.com/apk/res/android
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
何為命名空間呢?里面定義了各個(gè)類所用的屬性的定義。 android這個(gè)命名空間就對應(yīng)了/frameworks/base/core/res/res/values/attrs.xml文件中
定義的屬性值;而settings這個(gè)命名空間就是Settings應(yīng)用的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對應(yīng)的類名,attr則是類中的屬性.
這個(gè)是xml的命名空間,有了他,你就可以alt+/作為提示,提示你輸入什么,不該輸入什么,什么是對的,什么是錯(cuò)的,也可以理解為語法文件?;蛘哒Z法判斷器什么的。