2016-04-03 28 views
4

Geçerli uygulama temasına göre düğme seçicisi için farklı stilleri nasıl ayarlayabilirim?Düğme seçici için farklı temalar nasıl ayarlanır?

İşte uygulama tema renk olarak

<?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

açık cevap yerine @ renkli/color_theme1', örneğin 'gerçek bir tema rengi kullanmak olacaktır yardımcı olabilir 'colorPrimary' gibi. Bu söyleniyor, bu sadece api 21 ile çalışır veya –

cevap

2

color.xml içinde color_primary içindedir benim button_selector.xml olduğunu. bunu seçicinizde bu şekilde kullanabilirsiniz. Ancak, varsayılan durum için ve diğeri seçili_state için iki tane çizim dosyası oluşturmanız gerekir.

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> 

programlı olarak aşağıdakileri de yapmalısınız, böylece düğme seçili kalacaktır.

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

'un üstünde çalışır. Sadece seçilen temaya göre düğmeye basılan rengi değiştirmek istiyorum. –

+0

teşekkürler, açıklamanız için, asıl sorunuz bir şeydir, ancak sorunuzdaki stilleri kullanmaktan bahsettiniz. Sorunuzu düzenledi.Lütfen onaylayın. – HourGlass

1

Uygulamanızda dinamik seçiciyi kullanın, böylece gereksinimlerinize göre renk atayabilirsiniz.

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); 

Ya

Kontrol this veya this size

+0

Herhangi bir XML alternatifi var mı? –