本文介绍了凸起和平坦的按钮具有不同的背景和突出显示的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用com.android.support:appcompat-v7:23.0.1库创建应用程序.

I'm creating application using com.android.support:appcompat-v7:23.0.1 library.

我在values/styles.xml中定义应用程序主题:

I define application theme in values/styles.xml:

<style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="colorControlHighlight">@color/highlight_dark</item>
    <item name="colorButtonNormal">@color/primary</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColorSecondary">@color/secondary_text</item>
</style>

<style name="AppTheme" parent="BaseAppTheme"></style>

我将AppCompat Widget.AppCompat.Button.Colored样式用于凸起按钮,将Widget.AppComap.Button.Borderless.Colored样式用于平面按钮.

I use AppCompat Widget.AppCompat.Button.Colored style for raised button and and Widget.AppComap.Button.Borderless.Colored style for flat buttons.

colorAccent主题属性定义凸起的按钮背景颜色和平面按钮文本颜色,但我认为这是一个错误,因为这些颜色应由colorButtonNormal属性定义,就像对Widget.AppCompat.ButtonWidget.AppComap.Button.Borderless样式一样.

colorAccent theme attribute defines raised button background color and flat button text color but I suppose that it's a bug because these colors should be defined by colorButtonNormal attribute, as it does for Widget.AppCompat.Button and Widget.AppComap.Button.Borderless styles.

colorControlHighlight主题属性为两个按钮定义ripple颜色.

colorControlHighlight theme attribute defines ripple color for both buttons.

问题是:

  1. 如何使用不同颜色的凸起按钮?例如,我想要具有原色和强调色的按钮.
  2. 我的强调色不是浅色,所以凸起的按钮具有饱和背景,并且colorControlHighlight应该是浅色(#40ffffff).但是扁平按钮的背景透明,因此colorControlHighlight应该是深色的(#40000000).如何为凸起和扁平按钮设置不同的ripple颜色?
  1. How can I use raised buttons with different colors? For example, I want buttons with primary and accent colors.
  2. My accent color is not light, so raised button has saturated background and colorControlHighlight should be light (#40ffffff). But flat button has transparent background and colorControlHighlight should be dark (#40000000) for it. How can I set different ripple colors for raised and flat buttons?

我在下面添加了当前的解决方案,但我不禁感到自己错过了一些事情.

I added my current solution below, but I can't help feeling that I missed something.

推荐答案

经过研究和搜索,我为不同的凸起和扁平按钮定义了单独的主题:

After some research and googling I defined separate themes for different raised and flat buttons:

<style name="AppTheme.RaisedButton">
    <item name="buttonStyle">@style/Widget.AppCompat.Button.Colored</item>
</style>

<style name="AppTheme.RaisedButton.Primary">
    <item name="colorAccent">@color/primary</item>
</style>

<style name="AppTheme.RaisedButton.Accent">
</style>

<style name="AppTheme.FlatButton">
    <item name="buttonStyle">@style/Widget.AppCompat.Button.Borderless.Colored</item>
    <item name="colorControlHighlight">@color/highlight_light</item>
</style>

<style name="AppTheme.FlatButton.Primary">
    <item name="colorAccent">@color/primary</item>
</style>

<style name="AppTheme.FlatButton.Accent">
</style>

请注意,我使用的是buttonStyle属性而不是android:buttonStyle,因为它不适用于棒棒糖之前的设备.

Note, that I'm using buttonStyle attribute not android:buttonStyle because it will not work on pre-lollipop devices.

android:theme属性中使用这些主题:

Use these themes in android:theme attribute:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/my_button"
    android:theme="@style/AppTheme.FlatButton.Primary" />

这篇关于凸起和平坦的按钮具有不同的背景和突出显示的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:57