2014-12-08 45 views
0

我有一個簡單的文本按鈕
如果在android:文本我指的是它的字符串它的工作原理,因爲它假設。
參照字符串通過R.string

<Button 
      android:id="@+id/true_b" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button" /> // OK 



    <Button 
       android:id="@+id/true_b" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@id/R.string.true_button" /> // doesn`t output anything 

但是,如果我試圖通過R.string指我的字符串。「字符串名稱」什麼也沒有發生
解釋我請我在哪裏丟失的不便...

回答

1

有,你是做錯了一些事情。要引用XML中的字符串,您應該使用

android:text="@string/string_name" 

@string引用string.xml文件,string_name是您聲明的名稱。

這是string.xml所需的線會是什麼樣

<string name="string_name">This is a string you are referencing!r</string> 

而且我從來沒有嘗試過的命名與資源。分詞。這可能會導致錯誤,但我不是100%確定。

編輯:時,他說,你不能引用R.或android.R michal.z是非常正確的。來自XML的資源。您只在嘗試以編程方式引用資源時才使用它們。

+0

你只用不完R.id如果您在使用Java語言編寫的代碼。無論何時您想通過XML引用ANYING,您都可以使用上面的樣式。對於字符串使用use @ string/string_name。顏色是@ color/color_name。數組@陣列/ ARRAY_NAME等語法總是@ reference_file /變量名 – Dacotah 2014-12-08 04:00:00

+0

當我們參考編號,如:?** ... =「@ ID/any_id **,難道我們訪問R.id – Anarantt 2014-12-08 04:01:02

+0

從技術上說, ......在XML說=「@ ID/any_id」是一樣的說法(R.id.any_id)在Java中。 – Dacotah 2014-12-08 04:02:01

0

在XML你使用@+id/@id/指定哪個你View應該有或引用View以前在XML聲明的ID。您不能在XML代碼中引用R類,因爲實際上,R類是基於您在XML中定義的資源創建的。所以它在XML中無法訪問。