簡體   English   中英

通過R.string引用string

[英]Refering to string through R.string

我有一個帶有文字的簡單按鈕
如果在android:text中,我指的是字符串,則按預期進行。

<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引用我的字符串。“字符串名稱”將不會發生
請向我解釋我想念的地方...

您在這里做錯了幾件事。 要引用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%。

編輯:michal.z在說您無法引用R.或android.R時非常正確。 XML提供的資源。 僅在嘗試以編程方式引用資源時才使用它們。

在XML中,您可以使用@+id/@id/來指定View應該具有的ID或引用先前以XML聲明的View 您不能在XML代碼中引用R類,因為實際上R類是基於XML定義的資源創建的。 因此,它在XML中是不可訪問的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM