2011-08-03 62 views
0

您好形狀,我試圖將其包含在我的佈局中,但它不顯示?Android包含xml形狀

這是我的形狀XML「damage.xml」

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
shape="rectangle"> 
<solid color="#ff0000" /> 
<stroke width="3dp" color="#ff0000" /> 

這是我的,我想我需要包括在佈局形狀。

<?xml version="1.0" encoding="utf-8"?> 

<View android:id="@+id/damage" android:background="@layout/damage" 
    android:layout_width="5dip" android:layout_height="wrap_content"> 
</View> 

<ImageView android:id="@+id/icon" android:layout_width="60px" 
    android:layout_height="60px" android:layout_marginRight="6dip" 
    android:src="@drawable/placeholder" /> 

<LinearLayout android:orientation="vertical" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:padding="6dip"> 

    <TextView android:layout_width="fill_parent" android:id="@+id/title" 
     android:layout_height="wrap_content" android:text="Item Name" 
     android:textStyle="bold" android:singleLine="true" android:textSize="16sp" /> 

    <ImageView android:id="@+id/rating" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:src="@drawable/rating_five" /> 


    <LinearLayout android:orientation="horizontal" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"> 

     <TextView android:layout_width="wrap_content" android:id="@+id/range" 
      android:layout_height="wrap_content" android:text="Range" 
      android:textStyle="bold" android:singleLine="true" android:textSize="16sp" 
      android:paddingRight="5dip" /> 

     <TextView android:layout_width="fill_parent" android:id="@+id/condition" 
      android:layout_height="wrap_content" android:text="Condition" 
      android:textStyle="bold" android:singleLine="true" android:textSize="16sp" /> 

    </LinearLayout> 
</LinearLayout> 

但是沒有被抽?我哪裏錯了。其次,在視圖中設置形狀的寬度和heoght是否正確,或者我應該在damage.xml中做到這一點?例如

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
shape="rectangle"> 
<solid color="#ff0000" /> 
<size android:height="wrap_content" /> 
<size android:width="5dip" /> 
<stroke width="3dp" color="#ff0000" /> 

這裏編譯器抱怨WRAP_CONTENT。

任何幫助將不勝感激。

回答

1

嘗試將color="#ff0000"更改爲android:color="#ff0000"。這應該會導致矩形可見。一旦你這樣做,你會發現一個紅色的矩形出現,但它垂直填充父視圖。那是因爲你使用了android:layout_height="wrap_content",即使矩形沒有任何固定的高度。我會通過在您的佈局中使用android:layout_height的其他值來解決此問題。

此外,它似乎已將damage.xml放置在佈局目錄中。由於形狀是可繪製的,因此應將其放置在可繪製目錄中,並在佈局中將其作爲@layout/damage引用。

更新:

使用damage.xml下面的代碼使矩形顯示了對我很好。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#f0f000" /> 
    <stroke android:width="3dp" android:color="#ff0000" /> 
</shape> 
+0

android:color =「」似乎沒有工作。我也將damage.xml移到了drawable文件夾中。我想要矩形佔據列表行的全部高度,這就是我選擇wrap_content的原因。我還可以使用其他什麼價值? – Bear

+0

如果你想要矩形佔據列表行的全部高度,你應該使用layout_height =「match_parent」。 –

+0

現在很好用:-)非常感謝 – Bear