2016-03-05 120 views
19

我用这个风格改变背景色的我Button如何使用AppCompat设置禁用的按钮颜色?

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

而且在布局

<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"/> 

它的工作原理。但是当我在Button上拨打setEnabled(false)时,它保持相同的颜色。我如何管理这种情况?

回答

49

你没有正确使用Widget.AppCompat.Button.Colored风格。您正在使用父级样式(Widget.AppCompat.Button.Colored),但将其用作主题。这实际上意味着Widget.AppCompat.Button.Colored部分被完全忽略,您只需更改该按钮的默认颜色(该工作正常,但不处理禁用的情况)。

相反,你应该使用ThemeOverlay并分别应用Colored风格:

<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"/> 

正如this answer on using the Widget.AppCompat.Button.Colored style提到,残疾人颜色由colorButtonNormal控制,使颜色由colorAccent控制。通过使用ThemeOverlay.AppCompat.Dark,该textColor自动更改为黑色,这意味着你可能不需要自定义ThemeOverlay可言。

+0

定制'ThemeOverlay.AppCompat.Light'名为'AccentButton'对我来说是最清楚的方式。只有一个'colorButtonNormal'时,'Button'被禁用时'accentColor'与白色混合看起来是正确的。谢谢! – Alexandr

+0

而不是单独应用彩色样式,简化是将此样式声明移动到AccentButton主题中:将其定义为主题的buttonStyle。这样,每个按钮声明只需添加一个附加属性。 –

+0

@JoeBowbeer - 正如[选择按钮样式博客文章](https://medium.com/google-developers/choosing-a-button-style-with-purpose-and-intent-35a945e228d3)中提到的,凸起颜色按钮不一定是整个主题的标准,因此将其设置为基本'buttonTheme'可能有点多。 – ianhanniballake

1

而是采用颜色的按钮,你应该使用与选择的背景。下面是演示代码

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

当你编程改变了你需要行为方式:

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

OR

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

将接受的解决方案与自定义小部件结合在一起,我们可以通过设置alpha来显示禁用的按钮。这应该适用于任何按钮和文本颜色组合:

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

设置阿尔法是要走的路。 – VSG24

1

目前,我对Android 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> 

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

ianhanniballake'答案延伸完整的解决方案和Joe Bowbeer'comment:

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

而且无论你使用的按钮:

<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"/> 

这工作真的很好,我