1. 程式人生 > >Android 中從res/values/strings.xml中讀取具體的字串的方法

Android 中從res/values/strings.xml中讀取具體的字串的方法

正確的解法:
String mess = getResources().getString(R.string.mess_1);
另外一種:
you can simplify that to 
this.getString(R.string.some_id)
關鍵點android 字串比較,android 擷取字串,android 字串,android 字串陣列,androidpost字串,android 字串轉換,

字串資源的定義

檔案路徑:res/values/strings.xml

字串資源定義示例:

<?xml version="1.0" encoding="utf-8"?><resources
><string name="hello">Hello!</string></resources>

字串資源的呼叫

在 Layout XML 呼叫字串資源:

<TextView
    
android:layout_width="fill_parent"
    android:layout_height
="wrap_content"
    android:text
="@string/hello"/>

在 Activity 獲取字串資源:

this.getString(R.string.hello)

從 Context 獲取字串資源:

context.getString(R.string.hello)

從 Application 獲取字串資源:

application.getString(R.string.hello)