2012-12-03 31 views
1

我正在使用9patch圖像作爲我的ListView項目xml文件。強制9patch圖像以適合寬度,使用填充

但是,圖像背景似乎不符合佈局寬度。

Item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingBottom="10dp" 
    android:paddingTop="4dip" 
    android:orientation="horizontal" 
    android:background="@drawable/lvbg" > 

     <TextView 
    android:id="@+id/tvDate" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:text="Date" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/tvDate" 
    android:gravity="center" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:textColor="#31B6E7" /> 

</RelativeLayout> 

List.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" 
     android:cacheColorHint="#00000000" 
     android:divider="#FFFFFF" 
     android:listSelector="@drawable/list_selector" 
     android:paddingBottom="10dp" 
     android:saveEnabled="true" 
     android:smoothScrollbar="true" /> 

</LinearLayout> 

什麼我acheiving

enter image description here

我需要什麼:

enter image description here

這是我9patch圖像

enter image description here

是否有可能迫使TextView的內部,同時,該9patch圖像能夠填補一些DP填充屏幕寬度雙方。

在此先感謝。

回答

0

這可能與您如何膨脹佈局有關。你應該做這樣的事情在你的getView()梅索德如下(適配器):

@Override 
public View getView(int position, View convertView, ViewGroup parent) {  
     LayoutInflater Inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = Inflater.inflate(mResourceId, parent, false); 

現在重要的是你使用parent並設置它沒有附着(false)。通過這樣做,Android可以正確測量組件,從而爲它提供正確的尺寸。

相關問題