- @+id/text1
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
1. id並不是一定需要的,除非在java程式當中需要引用此控制項
2. "@+id/" 表示在這個activity當中如果沒有存在此id則新建一個。"@id/"則是表示使用已經定義過的id
3. id是以activity為範圍,在一個activity中id不可以重覆,但在不同activity當中可以容許重覆的id
4. 產生的id會在R.java類別當中產生一個public static final int 的constant來唯一的指名。不管你的應用程是裡面有多少個activity,使用了多少個同名的id,在R.jav當中所有同名的id會共用同一個constant。
R.java範例
package xxx.xxxxx.xxx;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int text1=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
- 放在res/raw裡面的xml檔案並不會被編輯成為binary file,也不會被放入.apk當中
- String Arrays
<!-- This file is /res/values/arrays.xml -->
<resources>
<string-array name="week">
<item >星期日</item>
<item >星期一</item>
<item >星期二</item>
<item >星期三</item>
<item >星期四</item>
<item >星期五</item>
<item >星期六</item>
</string-array>
</resources>
/** .java file for getting String Array from xml */
Resource res = activity-name.getResources();
String[] st = res.getStringArray(R.array.week);
- Plurals
<!-- This file is /res/values/students_in_class.xml -->
<resources>
<plurals name="students_in_class">
<item quantity = "one">There is 1 student</item>
<item quantity = "other">There are %d students</item> </plurals>
</resources>
/** .java for getting Plurals from xml */
String st1 = res.getQuantityString(R.plurals.students_in_class, 0, 0);
String st2 = res.getQuantityString(R.plurals.students_in_class, 1, 1);
String st3 = res.getQuantityString(R.plurals.students_in_class, 2, 2);
String st4 = res.getQuantityString(R.plurals.students_in_class, 100, 100);
輸出:
There are 0 students
There is 1 student
There are 2 students
There are 100 students
Plurals是為了語系上面有單複數型差異的語言所設計,在中文的用法上較少被使用,不過用它來產生鑲嵌一個整數的字串還是蠻方便的。
- String
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/values/strings.xml -->
<resources>
<string name="SimpleString">Simple String</string>
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don't work</string>
</resources>
Formatting strings
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Resources res = getResources()
;
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
Styling with HTML markup
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>
目前支援的tag只有
<b>
for bold text.<i>
for italic text.<u>
for underline text.
Note: 使用Eclipse編輯時,需要利用資源編輯視窗來編輯HTML字串,不要直接編寫.xml檔案。編輯器會將上面的標籤再做一次轉換變成
<string name="welcome">Welcome to <b>Android</b>!</string>
這樣才能正確顯示。
String htmlString = getApplicationContext().getString(R.string.welcome);
// convert it to a text span so that i can be set in a text view
Spanned textSpan = android.text.Html.fromHtml( htmlString );
其他相關Html格式的運用在http://developer.android.com/reference/android/text/Html.html。
沒有留言:
張貼留言