2016-07-29 95 views
0

我的問題是,當我想改變我的按鈕文本的名稱它給我的鐵桿錯誤,並說,你應該在Eclipse中使用字符串你好,每個人都可以幫助我解決這個問題嗎?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.companey.ali.Main" > 

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_world" 
     android:textSize="30sp" /> 

</RelativeLayout> 
+2

發佈完整的錯誤信息。你放的標題應該描述這個問題。 – m0skit0

回答

0

這是很好的做法,定義「的strings.xml」串文件時,將可在位置水庫>值>的strings.xml

<string name="button_text">Alireza</string> 

現在,在您的佈局文件做標記按鈕下列變化

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@strings/button_text" /> 

希望這有助於!

+0

這是一個建議,沒有答案。 – m0skit0

0

在res/values/strings.xml中定義按鈕名稱。例如,

<string name="button_text1">Benefits</string> 
    <string name="button_text2">Links</string> 

在你的佈局文件中,你可以使用它作爲按鈕的屬性,像這樣。

android:text="@strings/button_text1" 

如果您必須動態地更改代碼中的文本,使用基於您的需要

Button button = (Button) findViewById(<id_of_the_button>); 
    button.setText(context.getResources().getString(R.string.button_text1); 
or 
    button.setText(context.getResources().getString(R.string.button_text2); 

。 將字符串存儲在strings.xml中而不是硬編碼將幫助您在必須轉換爲不同語言環境時使用。這可能不是一個錯誤,只是一個警告。

0

它沒有給你錯誤,它只是給你警告,在strings.xml文件中你必須聲明字符串和使用字符串內xml使用字符串xml引用。要麼你忽略該警告或像下面這樣做。

轉到res文件夾,然後將值文件夾,裏面你會發現strings.xml檔案中打開該文件,其內部資源標籤做象下面這樣: -

<string name="alireza">Alireza </string> 

和內部的XML與@string/alireza像下面

使用
<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/alireza" /> 
+1

謝謝大家幫助我。 – alireza

+0

當我做到這一點,它給了我錯誤<按鈕 – alireza

+0

你可以請給我整個XML在你使用的,讓我知道它給出了什麼類型的錯誤。發送給我錯誤顯示在XML。 – Aditi

相關問題