2017-06-15 10 views
0

こんにちは私は動作するカスタムラウンドコーナーボタン(gps_button)を作ったが、状態の状態によってはボタンの色を変更したいが、色は変わらない。色変数はColors.xmlに格納されます色やカスタムボタンを変更

私の主な質問は、Color.xmlからカスタムボタンに色を設定する方法ですか?

これは私のコード主な活動です:

public void start_gps (View view){ 

gps_button = (Button) (findViewById(R.id.start_gps_button)); 

if ((log_state == true) && (start_gps_button_state == false)){ 

      start_gps_button_state = true; 
      gps_button.setBackgroundColor(R.color.startGpsColor); 
      gps_button.setText("STOP"); 
      voyagePoints.setText("0"); 

      Toast.makeText(this, "GPS Started", Toast.LENGTH_SHORT).show(); 

     }else if ((log_state == true) && start_gps_button_state == true){ 

      start_gps_button_state = false; 
      gps_button.setBackgroundColor(R.color.stopGpsColor); 
      gps_button.setText("START"); 

      Toast.makeText(this, "GPS Stopped", Toast.LENGTH_SHORT).show(); 
     } 
    } 

これは、カスタムボタンです:

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_pressed="true" > 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1" /> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <solid android:color="#1EB7F1"/> 
     </shape> 
    </item> 
    <item > 
     <shape android:shape="rectangle" > 
      <corners android:radius="10dip" /> 
      <stroke android:width="1dip" android:color="#1EB7F1" /> 
      <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1" /> 
     </shape> 
    </item> 
</selector> 

これはColor.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="colorPrimary">#A7E483</color> 
    <color name="colorPrimaryDark">#A7E483</color> 
    <color name="colorAccent">#FF4081</color> 
    <color name="stopGpsColor">#F9108E</color> 
    <color name="startGpsColor">#1EB7F1</color> 
</resources> 

答えて

0

ではthis questionを参照してください。 >あなたは、私はそれが動作するようになったbutton.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

+0

からそれが、おかげで「工場」ボタンに該当見えた、それは私の、カスタマーのボタンのために働くのだろうか? – Cardona

+1

はいそうです。単にgps_button.setBackgroundColor(getResources()。getColor(R.color.colorPrimary))を使用してください。 –

+0

@Cardonaすべてのボタン、カスタムボタンが含まれ、同じsetBackgroundColor()メソッドにアクセスできる –

関連する問題