【问题标题】:Circle shape drawable not rendering color on some devices圆形可绘制在某些设备上不呈现颜色
【发布时间】:2019-03-20 15:27:15
【问题描述】:

我在 Android 中的 drawable 遇到了麻烦,经过几天的深入研究,我决定向您寻求帮助。

为了满足我的需要,我创建了一个可绘制对象,用于在 Android Studio 的 XML 布局中使用它(文件名为 circle.xml),代码如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:innerRadius="0dp"
               android:shape="ring"
               android:thicknessRatio="2"
               android:useLevel="false">
            <solid android:color="#BABABE"/>
        </shape>
    </item>
</selector>

这里的颜色设置是通用的,但取决于我使用可绘制对象的情况。 这是我正在使用的ConstraintLayout

<android.support.constraint.ConstraintLayout
                android:id="@+id/constraintLayout11"
                android:layout_width="71dp"
                android:layout_height="71dp"
                android:layout_marginTop="24dp"
                android:background="@drawable/circle"
                android:backgroundTint="#D95F2B"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
</android.support.constraint.ConstraintLayout>

我用以下属性设置了一个新颜色:

android:background="@drawable/circle"
android:backgroundTint="#D95F2B"

设置background属性使圆圈出现在ConstraintLayout中,设置backgroundTint颜色使圆圈的颜色发生变化。

问题是,在某些设备上,它运行良好,但在其他一些设备上,backgroundTint 颜色未应用,圆圈仅保留可绘制对象中设置的颜色。

我知道我可以在我的活动代码中设置颜色,但这并不是我真正想要的。

有解决办法吗?如果需要,请随时问我一些问题。

提前非常感谢!

【问题讨论】:

  • 在某些设备上可以正常工作,但在某些其他设备上,backgroundTint颜色不适用....请提醒设备,即API级别,输入等等。
  • 问题是否仅限于设备的特定 Android 版本?
  • 它不适用于 API 级别 23 (Honor 5C) 的设备,另一个 API 级别 24 (Sony) 的设备。它在三星 (API 26)、Nexus 6P (API 22)、Pixel 2 (API 26) 上运行良好。这种行为是完全随机的。
  • 为什么不将backgroundTint 颜色设置为`
  • 因为我并不总是使用相同的颜色。一个组件是红色的,另一个是蓝色的,等等

标签: android xml-drawable


【解决方案1】:

很明显,drawable 应该是这样工作的,因此问题在于android:backgroundTint

该属性确实适用于 API 的 > 21,而您可能会在使用其他一些手机类型时遇到一些奇怪的行为。

因此,如果您需要有一个 通用 圆圈以在任何地方使用它,有一些解决方法:

  • 直接在可绘制对象&lt;solid android:color="@color/your_color"/&gt; 中使用颜色,这似乎对您的情况没有帮助。

  • 使用android:colorBackground 属性而不是backgroundTint, 然后您将其设置为样式,即:

风格:

<style name="YourStyle" parent="YourParent">
        <item name="android:background">@drawable/circle</item>
        <item name="android:colorBackground">@color/your_color</item>
    </style>

样式将与您的约束布局一起使用。

  • 使用包装视图,即:

Xml:-

<ConstraintLayout
android:background="@drawable/circle"

 <View
 android:background="@color/your_color"
 >
>

【讨论】:

  • 感谢您的回复。当我尝试用 android:backgroundColor 替换 backgroundTint 时,编译器给我错误:找不到属性 android:backgroundColor
  • @IbrahimAli 使用的样式在哪里,请描述性更强
  • 我尝试了样式方法和包装器方法,没有人在工作。 colorBackground 似乎对颜色没有任何影响。
  • @Vinestro 很抱歉,但至少 Wrapper 方式应该可以工作。
猜你喜欢
  • 2014-02-25
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多