与android:drawableBottom属性结合使用时不显示可绘制形状。
保存在res/drawable/gradient_box.XML的XML文件:与android:drawableBottom属性结合使用时不显示可绘制形状。,android,layout,drawable,shape,Android,Layout,Drawable,Shape,保存在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"
<?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
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"/>
将实际图像(例如a*.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上方时,形状尺寸无法自动确定。如果使用ImageView作为“android:src”属性的一部分来承载线条形状,则也会遇到相同的问题,除非将宽度和高度指定为形状xml的一部分。一种解决方法是将线条形状作为ImageView的“android:background”属性的一部分。通过这种方式,您可以使用ImageView的大小属性来“显示”线条形状。在这种情况下,无法设置匹配父宽度。对于我来说,当我以编程方式设置渐变可绘制宽度时,设置渐变可绘制宽度是有效的,例如在这里,即使我使用了
shape=“ring”,我也会遇到同样的问题
并设置其厚度
值。但是,正如您所提到的,这似乎不足以告诉形状如何绘制自身。我已经做了好几个小时了,尝试了填充、边距、背景色、图像和其他很多东西,但都没有效果。你的解决方案成功了。谢谢就我而言,这也不起作用。也许我做错了什么,但一般来说,在xml中尝试大小标记是最安全的选择。