【问题标题】:Android default button styling not workingAndroid默认按钮样式不起作用
【发布时间】:2015-09-01 18:33:46
【问题描述】:

我正在尝试设置我的样式以使所有按钮具有特定的颜色组合,特别是蓝色和白色文本。这是我的主要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>

在清单中:

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

color/primary 是深蓝色,color/primaryTextContrast 是白色。在 Lollipop 上,按钮看起来很完美。在 4.1 设备上,它是带有黑色文本的浅灰色。我找到的用于执行此操作的每个资源看起来都与我正在做的完全一样,所以我不知道我在这里缺少什么。

我在控制基本样式定义中的文本大小时也遇到了类似的问题。

更新:这里是颜色。

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

这里是 v19/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>

这里是 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>

我认为这些都不是让它在 5.1 上正常工作的原因。

【问题讨论】:

  • 您的代码在我的galaxy s3 mini (Android 4.1) 上完美运行,您确定您没有两个版本的styles.xml,而带有此代码的版本是> v16?
  • 我有两个版本,但这是 main/res/values 中的一个。
  • @Jones 我添加了其他样式文件。

标签: android android-styles


【解决方案1】:

使用 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>

然后使用来自android 命名空间的本机theme 属性在按钮中应用主题,正如Chris Banes 的this 精彩帖子所述。

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

【讨论】:

  • android:textColor已经在主题中定义了,为什么还要在Button声明中定义。
  • 难道没有办法改变应用程序中所有按钮的样式,这样我就不必在每个按钮上设置任何东西了吗?我认为这些样式文件应该能够做到这一点。
  • 由于某些未知原因,在主题中设置时,背景颜色无法正常工作。可能是因为将其定义到应用主题中会将其作为style 属性提供,而不是theme。我希望尽快解决这个问题。
  • 虽然对我来说这不是一个可行的选择,因为它需要我浏览我已经拥有的整个应用程序并更改每个Button,直到现在这是唯一可行的解​​决方案我发现。我真诚地希望 Google 尽快修复此错误。
  • 找到了解决方案,只需添加buttonStyle 不带android: 前缀,我的答案中的示例代码。
【解决方案2】:

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

为了让主题在 android lollipop 中正常工作,您需要扩展 ActionBarActivity而不是 Activity。 通过进行此更改,您的主题设置应该可以正常工作。 这对于其他人来说是通用的,对于较低版本的 android,您不应该在 item-name 定义中使用 android: 标签 `

【讨论】:

  • 我正在扩展 AppCompatActivity... 您能否提供一些支持文档,因为除非您扩展 ActionBarActivity,否则主题将无法工作,这听起来很奇怪。
  • 我知道这很奇怪,但经过数小时的调试,我发现扩展 ActionBarActivity 为我完成了这项工作。当我扩展 Activity 时,Theme.AppCompat 没有显示 Actionbar。所以我认为它可能会为你工作
  • 我认为这不是我的答案,因为 ActionBarActivity 已被弃用并被 AppCompatActivity 取代,这是我正在使用的。不过还是谢谢。
【解决方案3】:

我尝试添加不带android: 前缀的buttonStyle,它解决了问题。是的,很奇怪。

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

【讨论】:

  • 我对您的解决方案不满意。除按钮背景颜色外,所有样式均有效。
  • 我目前正在做其他事情,但我会试试看,谢谢。
  • 你刚刚救了我的命!为我工作:> compileSdkVersion 27 minSdkVersion 22 targetSdkVersion 27
猜你喜欢
  • 2012-10-24
  • 2017-10-28
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2015-07-05
  • 1970-01-01
相关资源
最近更新 更多