为什么9patch背景会在自定义列表视图中拉伸项目

时间:2017-01-16 18:49:53

标签: android xml android-layout listview nine-patch

我想为listview中的每个项目添加类似框架的背景,我制作了一个9patch文件,但它仍然延伸了图像的原始宽高比。

我的自定义适配器:

public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] itemname2;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] itemname2) {
    super(context, R.layout.mylist2, itemname);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
    this.itemname2 = itemname2;

}

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist2, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.item2);

    txtTitle.setText(itemname[position]);
    imageView.setImageResource(imgid[position]);
    extratxt.setText(itemname2[position]);
    return rowView;

};

我的xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/beksadsad">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:layout_gravity="center_vertical"
            android:gravity="center|end">
        <TextView
            android:id="@+id/item2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="5dp"
            android:padding="2dp"
            android:textColor="#000000"
            />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"
            android:layout_gravity="center_vertical"
            android:gravity="center">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:padding="5dp"
            android:layout_gravity="center_vertical" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:layout_gravity="center_vertical"
            android:gravity="center|start">
        <TextView
                android:id="@+id/item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="5dp"
                android:padding="2dp"
                android:textColor="#000000"

            />
        </LinearLayout>

</LinearLayout>

我用作背景的9patch图像

enter image description here

我希望我的背景适合我的项目的高度,这是由imageview(60dp)调节的。我试图将布局高度设置为100dp,但它没有任何改变。

解决:一切都很好,但图像太大(500x500)。

0 个答案:

没有答案