【问题标题】:Button style working in Android 5 but not Android 4按钮样式适用于 Android 5 但不适用于 Android 4
【发布时间】:2015-11-04 04:52:44
【问题描述】:

我有一个蓝色背景的按钮样式,在 API 22 中可以正常工作,但在 Android 4 中,相同的按钮在没有应用样式的情况下显示为深灰色。这是我的样式:

<style name="MyApp.Plain" parent="Theme.AppCompat.NoActionBar">
      <item name="android:windowBackground">@drawable/background</item>
      <item name="android:buttonStyle">@style/MyApp.Widget.Button</item>
</style>


  <style name="MyApp.Widget.Button" parent="@android:style/Widget.Button">
      <item name="android:background">@drawable/btn_blue</item>
      <item name="android:focusable">true</item>
      <item name="android:clickable">true</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textSize">14sp</item>
      <item name="android:textColor">#fff</item>
      <item name="android:gravity">center_vertical|center_horizontal</item>
   </style>

我的 btn_blue.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_disabled" android:state_enabled="false"/>
    <item android:drawable="@drawable/btn_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_normal_blue" android:state_enabled="true"/>

</selector>

和 btn_normal_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#368ac6"
        android:startColor="#0e58a4" />

    <corners android:radius="20dp" />

</shape>

此行为的原因可能是什么?我该如何解决?

编辑:这不适用于支持 v7:22.2.0,但适用于 v7:21.0.3。除了依赖项之外,我没有更改任何内容并将 AppCompatActivity 更改为 ActionBarActivity。

这可能是一个 Android 错误。

【问题讨论】:

  • 显示drawable/btn_blue的内容
  • 您是否有可能为 API 版本提供单独的文件夹,而这种样式仅在其中一个中?
  • @mcAdam331:不,我只有一个样式文件。
  • 所有其他属性都有效吗?是否可以点击? 4中的文字样式也是粗体吗?是否在后台失败?
  • 它是可点击的,但由于某种原因,文本全部大写,不是粗体,而是相同的颜色。

标签: android styles android-5.1.1-lollipop android-4.1-jelly-bean


【解决方案1】:

尝试buttonStyle而不是android:buttonStyle,因为这是Lollipop之前的AppCompat属性,所以它应该没有android前缀。

【讨论】:

  • 这应该被接受为答案,解决了我的问题!谢谢。
【解决方案2】:

我见过一些布局,其中属性被应用了两次,有和没有 Android 前缀。你可以试试这个:

<style name="MyApp.Widget.Button" parent="@android:style/Widget.Button">
      <item name="android:background">@drawable/btn_blue</item>
      <item name="background">@drawable/btn_blue</item>
      <item name="android:focusable">true</item>
      <item name="android:clickable">true</item>
      <item name="android:textStyle">bold</item>
      <item name="android:textSize">14sp</item>
      <item name="android:textColor">#fff</item>
      <item name="android:gravity">center_vertical|center_horizontal</item>
</style>

我目前正在尝试查找我之前在哪里看到的,以及它为什么起作用。如果有,请告诉我。

【讨论】:

  • 谢谢,我试过了,但不幸的是,在 API 10 上,按钮的背景仍然是灰色的。
  • @barq 我想值得一试。你能像卢卡斯问的那样发布 btn_blue 的内容吗?那么问题可能就在那里。
  • @barq 我找到了我的想法,它与支持库有关:stackoverflow.com/questions/18726865/… 我现在留下我的答案,以防我能够编辑它并在之后帮助你。
  • 是的,我也在看这个问题。不幸的是,这并没有奏效,至少不是靠它自己。
【解决方案3】:

尝试使用 AppCompat:

    <style name="MyApp.Widget.Button" parent="Base.TextAppearance.AppCompat.Button">
     <item name="android:background">@drawable/btn_blue</item>
      ...
    </style>

【讨论】:

  • 谢谢,但不幸的是,这也不起作用。
  • 你给按钮设置样式了吗?
  • 不,这是应用程序的一般按钮样式,因此应该没有必要。它在 Android 5 中无需这样做即可工作。
【解决方案4】:

@Michał Kisiel 的回答是正确的“尝试使用 buttonStyle 而不是 android:buttonStyle,因为这是 Lollipop 之前的 AppCompat 属性,所以它应该没有 android 前缀。” 我只想添加,例如,如果您以编程方式创建视图,您也应该放置 android:buttonStyle 和 buttonStyle,示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多