可绘制形状没有显示在组合使用与Android时:drawableBottom属性。组合、形状、属性、Android

2023-09-04 03:56:25 作者:是你的胖虎吗

在保存RES /绘制/ gradient_box.xml XML文件:

XML file saved at res/drawable/gradient_box.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开发者指南。有没有在它的错误。)。

(The above shape definition is is taken from then Android developer guide. There's no errors in it.).

让我们尝试使用它与一个TextView:

Let's try to use it together with a 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属性是不存在!但是,设置形状为背景的作品就好了:

The TextView displays as if the drawableBottom attribute wasn't there! However, setting the shape as the background works just fine:

<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也能正常工作。

Setting an actual image (e.g. a *.png) to the android:drawableBottom also works fine.

任何想法?

推荐答案

解决了这个问题!这个问题似乎是一个形状并不一定有内在的界限。也就是说,所产生的绘制不知道如何绘制自己!

Solved it! The problem seems to be that a shape does not necessarily have intrinsic bounds. That is, the resulting drawable doesn't know how to draw itself!

要解决这一问题,只需指定形状的大小,像这样的:

To solve this problem, simply specify the size of the shape, like this:

<?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的上面,形状尺寸不能被自动确定。

When the shape was specified as a background drawable for the TextView, its dimensions was known to be the same as the TextView dimensions. When telling the shape to go to the right or above the TextView, the shape dimensions could not be determined automatically.