2016-07-29 1 views
0

私の問題は、私は私のボタンのテキストの名前を変更したいとき、それは「のstrings.xml」内の文字列を定義することをお勧めしてくれハードコアエラーを与え、あなたは日食にこんにちはすべての人が私にこれを解決するのに役立つことができますか?

<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>あなたのレイアウトファイルで今すぐ

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

位置解像度>値で利用できるようになりますbuttonタグで次の変更を行います

<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内の文字列を使用する必要があることを警告しています。あなたはのstrings.xmlファイルは、そのファイルと内部リソースのタグを開く見つけるが、値フォルダにそのフォルダをresはする

ゴーは以下のように実行します -

<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

私がそれをしたときに私にエラーが出る

+0

あなたは私があなたが使っているどこのxmlを送ってくれて、私にそれが与えているエラーのタイプを知らせてくれますか? xmlにエラーを表示する。 – Aditi

関連する問題