【问题标题】:XML shape drawable not rendering desired color可绘制的 XML 形状未呈现所需的颜色
【发布时间】:2010-07-18 11:31:39
【问题描述】:

我定义了一个drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <solid android:color="#ffffffff" />
  <size android:width="60dp"
        android:height="40dp" />
</shape>

但是如何在layout定义中使用呢?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              >
    <ImageView
        android:src="@drawable/ic_title"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        />
</LinearLayout>

我仍然只能看到黑色背景。

已编辑:

原来我在drawable xmlns 声明中错过了一个:。现在这个例子可以在 Gingerbread 设备上运行 :)

【问题讨论】:

  • 愚蠢的问题,但ic_title 是您定义的可绘制对象的名称吗?另外,您可以在LinearLayout上设置android:background
  • 是的,ic_title 是我上面定义的drawable。我不想设置 android:background,这不是想要的效果。我可能会将更多 ImageView 放入 LinearLayout。

标签: android drawable


【解决方案1】:

在drawable中我使用这个xml代码来定义边框和背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <stroke android:width="4dp" android:color="#D8FDFB" /> 
  <padding android:left="7dp" android:top="7dp" 
    android:right="7dp" android:bottom="7dp" /> 
  <corners android:radius="4dp" /> 
  <solid android:color="#f0600000"/> 
</shape> 

【讨论】:

    【解决方案2】:

    我有一个类似的问题,发现如果你删除尺寸定义,它会出于某种原因工作。

    删除:

    <size
        android:width="60dp"
        android:height="40dp" />
    

    从形状。

    让我知道这是否有效!

    【讨论】:

    • 但是没有定义大小的形状,和填充ImageView的背景一样。
    • 是的。然后我会假设您需要一个容器来保存该图像视图,它将它自己的大小设置为您需要的大小 - 这就是我的示例所做的 - 即我声明一个具有大小的相对布局,然后将我的 imageView 放入其中.
    • 哈哈!我发现了你的问题:D 我希望你给我我的第一个荣誉;)你的形状定义中有一个错字。查看以下内容并发现 http: "http//schemas.android.com/apk/res/android" 后缺少的 ":"
    • 哦,这很奇怪。我应该检查这个错字是否真的出现在我的测试代码中。无论如何都要向你致敬:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多