2012-03-03 28 views
1

只要有在機器人教程的隨時隨地完成的Hello World,並試圖儘快添加一個按鈕,將鈕釦上的main.xml文件保存文件,我得到黃色預警安卓儘快添加一個按鈕,得到錯誤

[國際化]硬編碼字符串「按鈕」,應使用@string資源這個拿出保存文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 

</LinearLayout> 
+0

那只是一個提示沒有錯誤 – Michele 2012-03-03 19:58:50

+0

感謝您的回答 – percy 2012-03-03 20:57:53

回答

1

警告將不會從執行代碼阻止你了。它仍然告訴你在Android中不鼓勵在佈局中使用硬編碼的字符串。如果你想避免這種情況,你需要在你的res\values文件夾中創建文件strings.xml,並在其中加入以下內容:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="button_string">Button</string> 
</resources> 

以後改變佈局文件這樣的:

... 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_string" /> 

的慣例是出口您剛剛創建的文件中的所有文字字符串。這樣國際化你的代碼也會變得更容易。

順便說一句,你已經有在你的文本視圖中使用字符串的例子,所以只需在其中添加另一個常量。

+0

感謝鮑里斯的快速回復 – percy 2012-03-03 20:04:19