2016-04-03 11 views
4

現在のアプリのテーマに応じて、ボタンセレクターに異なるスタイルを設定するにはどうすればよいですか?アプリのテーマカラーはcolor.xmlでcolor_primaryであるのでここでボタンセレクターのテーマを設定するにはどうすればいいですか?

は私のbutton_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 
     <color android:color="@color/color_theme1"/> 
     </item> 
<!-- pressed --> 
    <item android:drawable="@color/transparent"/> 
<!-- default --> 

</selector> 
+0

助けることができるならば明白な答えは、例えば、 '@カラー/ color_theme1'するのではなく、実際のテーマカラーを使用することです'?colorPrimary'のように。これは言った、これはapi 21以上でのみ動作します –

答えて

2

です。このようなセレクタでこれを使うことができます。しかし、デフォルト状態用に2つの描画可能ファイルと、selected_state用に2つの描画可能ファイルを作成する必要があります。

button_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!--selected/pressed/focused --> 
<item  android:state_selected="true" 
      android:drawable="@drawable/button_selected" 
      /> 
    <item android:drawable="@drawable/button_default"/> 
<!-- default --> 

</selector> 

button_default.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<!--this is to give gradient effect --> 
<gradient android:angle="270" 
       android:startColor="@color/gray" 
       android:endColor="#@color/gray" 
       /> 
<!-- this will make corners of button rounded --> 
<corners android:topLeftRadius="5dip" 
       android:bottomRightRadius="5dip" 
       android:topRightRadius="5dip" 
       android:bottomLeftRadius="5dip"/> 

</shape> 

button_selected.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<!--this is to give gradient effect --> 
<gradient android:angle="270" 
      android:startColor="@color/color_primary" 
      android:endColor="#@color/color_primary" 
      /> 
<!-- this wil make corners of button rounded --> 
<corners android:topLeftRadius="5dip" 
       android:bottomRightRadius="5dip" 
       android:topRightRadius="5dip" 
       android:bottomLeftRadius="5dip"/> 

</shape> 

ボタンを選択したままにするために、プログラムで次の操作を行う必要があります。

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(v.isSelected()) 
       { 
        v.setSelected(false); 
       } 
       else 
       { 
        v.setSelected(true); 
       } 
      } 
     }); 
+0

私はちょうど選択されたテーマに応じてボタンの押した色を変更したいです。 –

+0

あなたの説明のために、あなたの実際の質問は何かですが、あなたの質問にスタイルを使用することについて述べました。あなたの質問を編集しました。 – HourGlass

1

必要に応じて色を割り当てることができるように、アプリでダイナミックセレクターを使用します。

StateListDrawable states = new StateListDrawable(); 
states.addState(new int[] {android.R.attr.state_pressed}, 
    getResources().getDrawable(R.drawable.pressed)); 
states.addState(new int[] {android.R.attr.state_focused}, 
    getResources().getDrawable(R.drawable.focused)); 
states.addState(new int[] { }, 
    getResources().getDrawable(R.drawable.normal)); 
imageView.setImageDrawable(states); 

それとも

チェックthisまたはthisはあなた

+0

任意のXMLの代替? –

関連する問題