【问题标题】:Padding on progressbar drawable is not applied不应用进度条可绘制的填充
【发布时间】:2014-08-26 14:43:15
【问题描述】:

我的进度条有这个drawable:

<item android:id="@android:id/background"  android:top="5dp">
    <shape>
        <gradient
            android:angle="270"
            android:centerColor="@color/gray11"
            android:centerY="0.75"
            android:endColor="@color/gray11"
            android:startColor="@color/gray11" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress"  android:top="5dp">
    <clip>
        <shape>
            <gradient
                android:angle="270"
                android:centerColor="@color/blue1"
                android:centerY="0.75"
                android:endColor="@color/blue1"
                android:startColor="@color/blue1" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress" android:top="5dp">

    <clip>
        <shape>
            <gradient
                android:angle="270"
                android:endColor="@color/blue1"
                android:startColor="@color/blue1" />
        </shape>
    </clip>
</item>

我想向下移动可绘制对象,所以我添加了 android:top="5dp" 但未应用填充(向下移动),我不明白为什么。

【问题讨论】:

    标签: android android-drawable android-progressbar


    【解决方案1】:

    这可能为时已晚,但我将这里发布给将来遇到此问题并偶然发现此问题的任何人。

    在 Marshmallow 之前,存在一个错误,即当 LayerDrawable 用作进度可绘制对象时,放置在 LayerDrawable 的子项上的某些属性会被忽略。该错误实际上在ProgressBar 中,其中android:progressDrawable 的值将通过tileify 方法运行,该方法将重新生成LayerDrawable,但无法复制某些属性(最明显的是填充)。

    您可以在 AOSP here 中查看解决此问题的方法。

    解决此问题最简单的解决方法是从代码中调用setProgressDrawable,而不是提供android:progressDrawable。因为setProgressDrawable 不调用tileify(并且因为tileify 在所有没有BitmapDrawable 的情况下都可能是一个空操作),所以您将保留您的填充和其他属性。

    但是请注意,如果您的进度可绘制对象包含bitmap/BitmapDrawable,您可能需要自己实现tileify 的一部分,以确保您获得与 xml 相同的平铺行为(或者,在Lollipop及以上,可以拨打setProgressDrawableTiled)。

    【讨论】:

    • 非常感谢!完美运行!记住不要把 android:progressDrawable 放在 xml 中,因为 setProgressDrawable() 不会有效果
    【解决方案2】:

    标签不考虑 padding 属性。它根本没有解析或包含它,因为在那里做它没有什么意义。

    应将正确设置的填充作为子元素包含在 标记中,如下所示:

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <padding 
                    android:top="5dp" />
                <gradient
                    android:angle="270"
                    android:endColor="@color/blue1"
                    android:startColor="@color/blue1" />
            </shape>
        </clip>
    </item>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多