问题描述
我正在使用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.Button
和Widget.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.
问题是:
- 如何使用不同颜色的凸起按钮?例如,我想要具有原色和强调色的按钮.
- 我的强调色不是浅色,所以凸起的按钮具有饱和背景,并且
colorControlHighlight
应该是浅色(#40ffffff
).但是扁平按钮的背景透明,因此colorControlHighlight
应该是深色的(#40000000
).如何为凸起和扁平按钮设置不同的ripple
颜色?
- How can I use raised buttons with different colors? For example, I want buttons with primary and accent colors.
- My accent color is not light, so raised button has saturated background and
colorControlHighlight
should be light (#40ffffff
). But flat button has transparent background andcolorControlHighlight
should be dark (#40000000
) for it. How can I set differentripple
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" />
这篇关于凸起和平坦的按钮具有不同的背景和突出显示的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!