我的进度条有这个可绘制对象:
<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"但未应用填充(向下移动),我不明白为什么。
最佳答案
这可能为时已晚,但我将此发布在这里,供将来遇到此问题并偶然发现此问题的任何人使用。
在 Marshmallow 之前,存在一个错误,当 LayerDrawable
用作进度可绘制对象时,放置在 LayerDrawable
子对象上的某些属性会被忽略。该错误实际上在 ProgressBar
中,其中 android:progressDrawable
的值将通过 tileify
方法运行,该方法将重新生成 LayerDrawable
但无法复制某些属性(最显着的是填充)。
您可以在 AOSP here 中查看对此的修复.
此问题最简单的解决方法是从代码中调用 setProgressDrawable
而不是提供 android:progressDrawable
。因为 setProgressDrawable
不调用 tileify
(并且因为 tileify
可能对于所有没有 BitmapDrawable
),您将保留填充和其他属性。
但是请注意,如果您的 progress drawable 包含 bitmap
/BitmapDrawable
,您可能需要自己实现部分 tileify
以确保您将获得与 xml 相同的平铺行为(或者,在 Lollipop 及更高版本上,您可以调用 setProgressDrawableTiled
)。
关于android - 不应用进度条可绘制的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508703/