Android默认按钮样式不起作用

时间:2023-01-27 23:34:56

I'm trying to set up my styles to make all buttons a particular color combination, specifically blue with white text. Here's my main styles.xml:

我正在尝试建立我的风格,使所有的按钮都有一个特定的颜色组合,特别是蓝色和白色的文本。这是我的主要styles.xml:

<resources>
    <style name="CustomTheme" parent="MaterialDrawerTheme.Light.DarkToolbar">
        <!-- various items -->

        <item name="android:buttonStyle">@style/ButtonStyle</item>
    </style>

    <!-- a couple of other styles -->

    <style name="ButtonStyle" parent="android:style/Widget.Button">
        <item name="android:textSize">19sp</item>
        <item name="android:textColor">@color/primaryTextContrast</item>
        <item name="android:background">@color/primary</item>
    </style>
</resources>

And in the manifest:

而在清单:

 <application
        android:name=".CustomApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/application_name"
        android:theme="@style/CustomTheme">

color/primary is dark blue, and color/primaryTextContrast is white. On Lollipop, the button looks perfect. On a 4.1 device, it's light gray with black text. Every resource I've found for doing this looks exactly like what I'm doing so I don't know what I'm missing here.

color/primary是深蓝色,color/primaryTextContrast是白色。在棒棒糖上,这个按钮看起来很完美。在4.1设备上,它是浅灰色和黑色文本。我所找到的每一种资源都与我所做的完全相同,所以我不知道我在这里遗漏了什么。

I'm having a similar issue with controlling text size in the base style definition as well.

在基本样式定义中控制文本大小也有类似的问题。

Update: here are the colors.

更新:这是颜色。

<resources>
    <color name="primary">#3F51B5</color>
    <color name="dark">#303F9F</color>
    <color name="accent">#FFCA28</color>
    <color name="background">@android:color/white</color>
    <!-- Color for text displayed on top of the primary or dark color -->
    <color name="primaryTextContrast">@android:color/white</color>
    <!-- Color for text displayed on the background color (which I think will always be white) -->
    <color name="basicText">@color/primary</color>
    <!-- Color for text displayed on the accent color -->
    <color name="accentText">#303F9F</color>
</resources>

Here's v19/styles.xml:

这是第十九节/ styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="FullscreenTheme" parent="MaterialDrawerTheme.Light.DarkToolbar.TranslucentStatus">
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

Here's v21:

v21:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="CustomTheme">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>
</resources>

I don't think either of these is what's making it work properly on 5.1.

我不认为这两个都能使它在5.1中正常工作。

3 个解决方案

#1


14  

Using AppCompat 22.1.+ (22.2.0 should work too), I defined a Style like this:

使用AppCompat 22.1。+(22.2.0也可以),我定义了这样的风格:

<style name="MyApp.Button.Red" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/primary</item>
    <item name="android:colorButtonNormal">@color/primary</item>
    <item name="android:textColor">@android:color/white</item>
</style>

and then applied the theme in a button using the native theme attribute from android namespace, as said in this awesome post from Chris Banes.

然后使用android命名空间的native theme属性将主题应用到一个按钮中,就像Chris Banes在这篇很棒的文章中说的那样。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_button"
    android:theme="@style/MyApp.Button.Red" />

#2


2  

I tried adding buttonStyle without the android: prefix and it solved the problem. Yeah, weird.

我尝试在没有android:前缀的情况下添加buttonStyle,这就解决了问题。是的,很奇怪。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="buttonStyle">@style/ButtonStyle</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>

#3


-2  

gradle:compile 'com.android.support:appcompat-v7:22.2.0'

gradle:编译com.android.support:appcompat-v7:22.2.0

For theme to work properly in android lollipop, you need to extend ActionBarActivityinstead of Activity. By doing this change,your theme setting should work properly. This is general for other people that for lower version of android,you should not use android: tag in item-name definition `

要使主题在android棒棒糖中正常工作,您需要扩展ActionBarActivityinstead of Activity。通过这样的更改,您的主题设置应该能够正常工作。这对于其他用户来说是很普遍的,对于较低版本的android,不应该使用android:项目名称定义中的标签'

#1


14  

Using AppCompat 22.1.+ (22.2.0 should work too), I defined a Style like this:

使用AppCompat 22.1。+(22.2.0也可以),我定义了这样的风格:

<style name="MyApp.Button.Red" parent="Base.Widget.AppCompat.Button">
    <item name="colorButtonNormal">@color/primary</item>
    <item name="android:colorButtonNormal">@color/primary</item>
    <item name="android:textColor">@android:color/white</item>
</style>

and then applied the theme in a button using the native theme attribute from android namespace, as said in this awesome post from Chris Banes.

然后使用android命名空间的native theme属性将主题应用到一个按钮中,就像Chris Banes在这篇很棒的文章中说的那样。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/sign_up_button"
    android:theme="@style/MyApp.Button.Red" />

#2


2  

I tried adding buttonStyle without the android: prefix and it solved the problem. Yeah, weird.

我尝试在没有android:前缀的情况下添加buttonStyle,这就解决了问题。是的,很奇怪。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="buttonStyle">@style/ButtonStyle</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
</style>

#3


-2  

gradle:compile 'com.android.support:appcompat-v7:22.2.0'

gradle:编译com.android.support:appcompat-v7:22.2.0

For theme to work properly in android lollipop, you need to extend ActionBarActivityinstead of Activity. By doing this change,your theme setting should work properly. This is general for other people that for lower version of android,you should not use android: tag in item-name definition `

要使主题在android棒棒糖中正常工作,您需要扩展ActionBarActivityinstead of Activity。通过这样的更改,您的主题设置应该能够正常工作。这对于其他用户来说是很普遍的,对于较低版本的android,不应该使用android:项目名称定义中的标签'