2016-03-05 18 views
19

Używam tego styl zmienić tle kolor moich Button:Jak ustawić wyłączony kolor przycisku za pomocą AppCompat?

<style name="AccentButton" parent="Widget.AppCompat.Button.Colored"> 
    <item name="colorButtonNormal">@color/colorAccent</item> 
    <item name="android:textColor">@color/white</item> 
</style> 

aw układ:

<Button 
     android:id="@+id/login_button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/fragment_login_login_button" 
     app:theme="@style/AccentButton"/> 

to działa. Ale kiedy zadzwonię pod numer setEnabled(false) na tym Button, zachowuje on ten sam kolor. Jak mogę zarządzać tą sprawą?

Odpowiedz

49

nie są prawidłowo stosowany styl Widget.AppCompat.Button.Colored . Używasz stylu macierzystego (Widget.AppCompat.Button.Colored), ale stosujesz go jako motyw. Oznacza to, że część Widget.AppCompat.Button.Colored jest całkowicie ignorowana i zamiast tego zmieniasz tylko domyślny kolor przycisku (który działa, ale nie obsługuje wyłączonego przypadku).

Zamiast tego należy użyć ThemeOverlay i zastosować styl Colored oddzielnie:

<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark"> 
    <!-- customize colorButtonNormal for the disable color --> 
    <!-- customize colorAccent for the enabled color --> 
</style> 

<Button 
    android:id="@+id/login_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/fragment_login_login_button" 
    android:theme="@style/AccentButton" 
    style="@style/Widget.AppCompat.Button.Colored"/> 

Jak wspomniano w this answer on using the Widget.AppCompat.Button.Colored style, niepełnosprawnych kolor jest kontrolowany przez colorButtonNormal i włączona kolor jest kontrolowany przez colorAccent. Przy użyciu ThemeOverlay.AppCompat.Dark, textColor jest automatycznie zamieniany na ciemny, co oznacza, że ​​może wcale nie być potrzebny niestandardowy ThemeOverlay.

+0

Niestandardowy 'ThemeOverlay.AppCompat.Light' nazwie' AccentButton' jest najwyraźniejszy sposób dla mnie. Z jednym 'colorButtonNormal' wygląda dokładnie tak, jak' akcentColor' jest mieszany z białym, gdy 'Button' jest wyłączony. Dzięki! – Alexandr

+0

Zamiast stosowania kolorowego stylu oddzielnie, uproszczenie polega na przeniesieniu deklaracji stylu do motywu AccentButton: poprzez zdefiniowanie go jako przycisku buttonType kompozycji. W ten sposób do każdego zgłoszenia przycisku należy dodać tylko jeden dodatkowy atrybut. –

+0

@JoeBowbeer - jak wspomniano w poście na blogu [Wybór postu w stylu przycisku] (https://medium.com/google-developers/choosing-a-button-style-with-purpose-and-intent-35a945e228d3), podniósł kolor Przyciski nie muszą być normą w całym twoim motywie, więc ustawienie go jako podstawowego przycisku 'buttonTheme 'może być trochę za dużo. – ianhanniballake

1

zamiast używać koloru przycisku, należy użyć tła z selektorami. Oto kod demo

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="true"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/yourEnabledColor" /> 
     </shape> 
    </item> 
    <item android:state_enabled="false"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/yourDisabledColor" /> 
     </shape> 
    </item> 
</selector> 
3

Po zmianie programowo trzeba działać w ten sposób:

button = new Button(new ContextThemeWrapper(ActiVityName.this, R.style.AccentButton)); 

LUB

if (button.isEnabled()) 
    button.getBackground().setColorFilter(Color.Black, PorterDuff.Mode.MULTIPLY); 
else 
    button.getBackground().setColorFilter(null); 
6

Łącząc przyjęte rozwiązanie z niestandardowym widżetem możemy mieć przycisk, który wydaje się wyłączony przez ustawienie alfa. To powinno działać w przypadku dowolnej kombinacji kolorów przycisków i tekstu:

public class ButtonWidget extends AppCompatButton { 

    public ButtonWidget(Context context) { 
     super(context); 
    } 

    public ButtonWidget(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ButtonWidget(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void setEnabled(boolean enabled) { 
     setAlpha(enabled ? 1 : 0.5f); 
     super.setEnabled(enabled); 
    } 

} 
+0

Ustawienie alfa jest drogą do zrobienia. – VSG24

1

Obecnie używam następujących ustawień dla Androida API 15+.

/res/color/btn_text_color.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="#42000000" android:state_enabled="false" /> 
    <item android:color="#ffffff" /> 
</selector> 

/res/values/styles.xml

<style name="ColoredButton" parent="Widget.AppCompat.Button.Colored"> 
    <item name="android:textColor">@color/btn_text_color</item> 
</style> 

i

<Button 
    android:id="@+id/button" 
    style="@style/ColoredButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="button" /> 
0

pełna roztwór odchodzące od ianhanniballake 'i odpowiedź Joe Bowbeer 'komentarz:

/res/values ​​/ styles.xml

<style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark"> 
    <!-- customize colorAccent for the enabled color --> 
    <!-- customize colorControlHighlight for the enabled/pressed color --> 
    <!-- customize colorButtonNormal for the disabled color --> 
    <item name="android:buttonStyle">@style/Widget.AppCompat.Button.Colored</item> 
</style> 

I gdziekolwiek użyć przycisku:

<Button 
    android:id="@+id/login_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/fragment_login_login_button" 
    android:theme="@style/AccentButton"/> 

który pracował naprawdę miłe dla mnie

Powiązane problemy