Android 将xml文件用作可绘制资源

Android 将xml文件用作可绘制资源,android,drawable,Android,Drawable,我的可绘制目录中有一个名为off.xml的xml文件(应该是一个红色圆圈) 在我的主页上有以下xml <LinearLayout android:layout_width="match_parent" android:layout_height="50dip" android:orientation="horizontal"> <View android:layout_width="40dip" android:layout_height="0di

我的可绘制目录中有一个名为off.xml的xml文件(应该是一个红色圆圈)


在我的主页上有以下xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="50dip" android:orientation="horizontal">    
<View android:layout_width="40dip"
    android:layout_height="0dip"  android:layout_weight="1"
    android:background="@drawable/off" />
</LinearLayout>

我发现错误“无法将@drawable/off转换为drawable。红色圆圈不显示。 (我在那里放了一个按钮,以确保另一个小部件会显示)

是什么导致了这个错误。我从这个论坛的另一篇文章中得到了这个例子,它应该是有效的。
谢谢,Gary

您的@drawable/off布局应该是

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
  <item><shape android:shape="oval">
    <gradient android:startColor="#FFFF00" android:endColor="#FFFF00"
      android:angle="270"/>
    </shape></item>
</selector>

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="270"
        android:endColor="#FFFF0000"
        android:startColor="#FFFF0000" />

</shape>


两个问题:您的可绘制视图中缺少关闭标记,视图的布局高度为0或方向错误。

这不会绘制椭圆形。这与我所做的没有什么不同。(我尝试过,但仍然得到相同的错误).我没有错过标签。这个论坛中有些bug有时无法识别粘贴的线条。你的回答让我想到了高度和重量,结果我不得不将布局重量放在线性布局上,而不是视图上。我在Eclipse中仍然会遇到错误,但红色圆圈显示出来。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="270"
        android:endColor="#FFFF0000"
        android:startColor="#FFFF0000" />

</shape>