0

ユーザーがラジオ ボタンを使用してアプリの色を変更できるようにしようとしています。ユーザーがラジオ ボタンをクリックすると、メソッドが呼び出されます。ここで、5 つの xml ファイルの背景色を x/y/z に変更したいと考えています。しかし、xml ファイル自体を参照する方法がわかりません。id がないためです。文字列リソースに 5 つの色があるため、xml ファイルを参照できるようになると、背景色が変更されます。 (文字列を置き換える色の 16 進表記)。それはできますか、それともメリーゴーラウンドに戻る必要がありますか??

 public void rbbgColourClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();

            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.rbbgcolour_grey:
                    if (checked)
                        // Change to grey
                    break;
                case R.id.rbbgcolour_blue:
                    if (checked)
                        // Change to blue
                    break;


                case R.id.rbbgcolour_white:
                    if (checked)
                        // Change to white
                    break;



            }



  }

入れたら

   LinearLayout one = (LinearLayout) findViewById(R.layout.preferences);
                        one.setBackgroundColor(0xff888888);

なぜそこにあるのか理解できません。

ありがとう

4

3 に答える 3

1

使用するsetBackgroundResource()

例:

view.setBackgroundResource(R.id.rbbgcolour_grey);
于 2013-10-26T19:22:28.923 に答える
1
LinearLayout ll = (LinearLayout) findViewById(R.id.myLinearLayout);
ll.setBackgroundColor(getResources().getColor(R.color.red));
于 2013-10-26T19:23:47.670 に答える
1

これは、SharedPreferences を使用して行うことができます。背景色の 16 進数コードをプリファレンスとして保存できます。ユーザーがラジオ ボタンをクリックすると、SharedPreferences の 16 進数コードを変更する必要があります。こんな感じです……。

各アクティビティの onCreate で...

SharedPreferences sp = getSharedPreferences("MyPref", 0);
String hexaColor = sp.getString("hexa", "#000000"); //default color will be #000000

次に、これをそれらのアクティビティの背景色として設定します。

ユーザーがラジオボタンをクリックすると、これを行います...

SharedPreferences sp = getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString("hexa", "new hexa code");
editor.commit();

これがあなたを助けることを願っています。

于 2013-10-26T20:03:51.197 に答える