强制9patch图像适合宽度,带填充

时间:2012-12-03 04:45:08

标签: android nine-patch

我正在为我的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>

我正在做什么

enter image description here

我需要什么:

enter image description here

这是我的9patch图片

enter image description here

是否可以在内部强制使用textview,同时9patch图像能够在侧面填充几个dp填充来填充屏幕宽度。

提前致谢。

1 个答案:

答案 0 :(得分: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可以正确地测量组件,从而为其提供正确的尺寸。