2015-11-30 47 views
0

我想改變色盲選項菜單中的顏色,我想有兩個strings.xml文件,並在按鈕按下時在它們之間切換。 ImageButton改變顏色在strings.xml中聲明的正確方法是什麼?如何更改android應用程序中的顏色?

final Switch colorsChange= (Switch) findViewById(R.id.switch_colors); 
    colorsChange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       // Color blind friendly colors here 

      } else { 
       // Normal RGB colors here 
       getResources().getColor(R.color.defaultColors); 
      } 
     } 
    }); 

我想繼續跨越如果可能的話所有活動

+0

發佈您的代碼,請 –

+1

發佈添加你想要的顏色到你的資源

  • 給行動使用android:onClick="methodName"
  • 在Java方法您的按鈕,使色彩。我想在應用 – TroubleSome

  • +0

    顏色的兩種變化'如果(器isChecked){// 色盲友好的色彩在這裏 ImageButton.setBackgroundColor(Color.parseColor(的getString(R.string.red)) }其他{ //正常RGB顏色在這裏 getResources()。getColor(R.color.defaultColors); }' –

    回答

    0

    button.setColor(Color.RED)這些設置; 或者你可以使用button.setColor(code color);

    0

    你應該colors.xml申報顏色

    但儘管如此, 在string.xml應聲明<string name="red">#ff0000</string>

    ImageButton.setBackgroundColor(Color.parseColor(getString(R.string.red));//if using string.xml 
    or 
    ImageButton.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));//if using colors.xml 
    

    ,或者:

    ImageButton.setBackgroundColor(Color.RED); // From android.graphics.Color 
    

    或者更多親技巧:

    ImageButton.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB 
    

    Documentation

    0

    你應該定義在color.xml顏色(在 「值」 目錄)

    <?xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <color name="color_name">#e6e6e6</color> 
    </resources> 
    

    ,並參考吧:

    1. 在XML文件:

      @ color/color_name

    2. Java類中:

      getResources()。getColor(R.color.color_name);

    0
    • 使用getResources().getColor(R.color.idname);
    +0

    謝謝,但並不只是給出一個變體?如果用戶選擇了 – TroubleSome

    +0

    ,我需要兩個變量並在它們之間切換您可以在方法中添加一個變量,該變量將用戶輸入作爲值 –

    相關問題