保存在 res/drawable/gradient_box.xml 的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
(以上形状定义取自当时的Android开发者指南,没有错误)。
让我们尝试将它与 TextView 一起使用:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text with some crazy rectangle shape below it."
android:drawableBottom="@drawable/gradient_box"/>
TextView 显示时就好像 drawableBottom 属性不存在一样!但是,将形状设置为背景效果很好:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text with crazy background"
android:background="@drawable/gradient_box"/>
将实际图像(例如 *.png)设置为 android:drawableBottom 也可以正常工作。
有什么想法吗?
最佳答案
解决了!问题似乎是形状不一定具有内在边界。也就是说,生成的可绘制对象不知道如何绘制自己!
要解决这个问题,只需指定形状的大小,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
<size android:width="xxdp"
android:height="xxdp"/>
</shape>
当形状被指定为 TextView 的可绘制背景时,其尺寸已知与 TextView 尺寸相同。当告诉形状移动到 TextView 的右侧或上方时,无法自动确定形状尺寸。
关于android - 与 android :drawableBottom attribute. 结合使用时不显示可绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6859525/