【问题标题】:Shape item doesn't apply for buttons on some devices形状项目不适用于某些设备上的按钮
【发布时间】:2016-09-20 07:49:31
【问题描述】:

我想让我的按钮有不同颜色的环形。我做到了,它正在我正在调试的设备上工作。 代码 black_round_button.xml 位于 drawable-v21

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring">
<solid android:color="@color/black" />

按钮代码是

<Button
  android:id="@+id/button31"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:background="@drawable/black_round_button"
  android:onClick="countClick"
  android:text="@string/zero" />

在我调试的设备(Android 5.0.2)上看起来不错: black ring shape

但是!!!当我开始在其他设备(android 4.4.4、Android 5.1、其他)上调试时,我发现我的任何形状环都不起作用。看起来像: no black ring shape

所以,我不知道为什么会这样。昨天我在 5 个不同的设备上测试它(其中 3 个是虚拟的,2 个是真实的),只有在我的设备上它才能按我的预期工作。

【问题讨论】:

    标签: android xml shape ring


    【解决方案1】:

    那是因为您将 xml 文件放在 drawable-v21 文件夹中。这仅适用于 API >= 21!您还应该有一个“drawable”文件夹(没有“-v21”扩展名),尝试在其中添加 xml 文件。如果该文件夹不存在,只需在与“drawable-v21”文件夹相同的级别上创建它即可。

    【讨论】:

    • 啊哈,是的,当我问这个问题时,我也开始思考这个问题。问题是,在我的 Android 工作室中,我将它们放在 drawable 文件夹中,但在 github 上,它们位于 drawable-v21 中。好的,感谢您的建议,今天将尝试此解决方案。
    • 你不会相信我,但这并没有帮助 =( 我将我所有的 xml 形状从 drawable-v21 移动到仅可绘制,什么都没有。看起来我需要玩 gradle 并构建 bersions
    • 不,这不是一个案例 =( 我在 android 6.0 上使用 API 23 调试它,它也不起作用。
    【解决方案2】:

    所以问题不仅对于 Android 4.4 (API 19) 是可重现的,对于 Android 6 (API23) 等旧版本也是可重现的,所以问题是我的环形没有必要的属性。据我了解,从 API22 开始,您需要添加

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="17dp"
    android:shape="ring"
    android:thickness="5dp"
    android:useLevel="false">
    

    这将解决 API22-API24 的问题。

    我不确定是否有必要将 xml 文件从 drawable-v21 文件夹移动到仅可绘制(因为看起来环形在低于 API21 的版本中不起作用,您需要在这种情况下使用“椭圆形”,但我不确定)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 2016-02-06
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多