6. 定义资源(XML)
属性-值 (attr-value)
• 定义属性
• styleable (schema)
• 赋值
• Layout, Style
• value type
– int, boolean, float, String, reference
• E.g. TextView
7. 资源定义 - Case study
android.widget.TextView
platform_frameworks_base/core/res/res/values/attrs.xml
platform_frameworks_base/core/java/android/widget/TextView.ja
va
<declare-styleable name="TextView">
<!-- Determines the minimum type that getText() will public TextView(Context context,
return. AttributeSet attrs,
The default is "normal". int defStyle) { AttributeSet
Note that EditText and LogTextBox always return Editable, super(context, attrs, defStyle);
even if you specify something less powerful here. --> …
<attr name="bufferType"> a = theme.obtainStyledAttributes(
<!-- Can return any CharSequence, possibly a
TypedArray attrs,
Spanned one if the source text was Spanned. --> com.android.internal.R.styleable.TextView, defStyle, 0);
<enum name="normal" value="0" /> ……
<!-- Can only return Spannable. -->
<enum name="spannable" value="1" /> int n = a.getIndexCount();
<!-- Can only return Spannable and Editable. --> for (int i = 0; i < n; i++) {
<enum name="editable" value="2" /> int attr = a.getIndex(i);
</attr> switch (attr) {
<!-- Text to display. --> ….
<attr name="text" format="string" case com.android.internal.R.styleable.TextView_text:
localization="suggested" /> text = a.getText(attr);
<!-- Hint text to display when the text is empty. --> break;
<attr name="hint" format="string" /> ….
<!-- Text color. --> }
<attr name="textColor" /> …..
<!-- Color of the text selection highlight. --> a.recycle();
<attr name="textColorHighlight" />
<!-- Color of the hint text. -->
<attr name="textColorHint" />