无法更改ListView项的大小

时间:2017-07-22 15:02:43

标签: android listview

我正在尝试更改ListView项目的高度。不幸的是,我的ListView适配器将我的项目大小调整为加载到其中的图像的大小。

我从MainActivity获得了ArrayList。这是我的ListAdapter:

public class Customlist extends ArrayAdapter<HashMap<String, String>> {

    private Context context;
    private ArrayList<HashMap<String, String>> contactlist;

    public Customlist(Context context, ArrayList<HashMap<String, String>> contactlist) {
        super(context, R.layout.model, contactlist);
        this.context = context;
        this.contactlist= contactlist;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View rowView= inflater.inflate(R.layout.model, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.namelist);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imagelist);

        // Contact
        HashMap<String, String> contact = contactlist.get(position);

        String name = contact.get("name");
        String imageurl = contact.get("imageurl");

        txtTitle.setText(name);
        Picasso.with(context).load(imageurl).into(imageView);

        return rowView;
    }

}

这是我的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    android:padding="0dp">

    <ImageView
        android:id="@+id/imagelist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:scaleType="fitXY"
        android:src="@drawable/logoooo" />

    <TextView
        android:id="@+id/namelist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="13dp"
        android:layout_marginStart="13dp"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:text="Neuraum "
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

之后的getView方法中
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View rowView= inflater.inflate(R.layout.model, parent, true);

添加:

rowView.getLayoutParams().height = 100; // or whatever you want for your height

更新 改变这个

View rowView= inflater.inflate(R.layout.model, null, true);

到这个

View rowView= inflater.inflate(R.layout.model, parent, true);

和这个

if (rowView != null){
    rowView.getLayoutParams().height = 100; // or whatever you want for your height
}

答案 1 :(得分:0)

你可以这样做

View rowView= inflater.inflate(R.layout.model, null, true);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1000);
rowView.setLayoutParams(params);

// ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,height);