与 android:drawableBottom 属性结合使用时,可绘制形状不会显示。

发布于 2024-11-26 13:42:16 字数 1209 浏览 1 评论 0原文

XML文件保存在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开发人员指南。其中没有错误。)。

让我们尝试将它与 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 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>

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

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"/>   

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"/>

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国际总奸 2024-12-03 13:42:16

解决了!问题似乎在于形状不一定具有内在界限。也就是说,生成的可绘制对象不知道如何绘制自身!

要解决此问题,只需指定形状的大小,如下所示:

<?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 的右侧或上方时,无法自动确定形状尺寸。

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>

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.

奢华的一滴泪 2024-12-03 13:42:16

如果您使用 ImageView 将线条形状作为“android:src”属性的一部分来托管,那么您也会遇到相同的问题,除非您将宽度和高度指定为形状 xml 的一部分。一种解决方法是将线条形状作为 ImageView 的“android:background”属性的一部分。这样,您就可以利用 ImageView 的尺寸属性来“显示”线条形状。

If you are using an ImageView to host the line shape as part of the "android:src" attribute, you will also run into the same problem unless you specify the width and height as part of the shape xml. One workaround is to host the line shape as part of the "android:background" attribute of ImageView. This way, you can make use of the size attributes of the ImageView for the line shape to "show" through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文