Listview项目高度为25%

时间:2016-06-10 15:56:38

标签: android listview height android-arrayadapter

我有一个listview,并使用自定义适配器填充它。我希望我的所有listview项目的高度都是25%。我尝试使用没有结果的布局权重。有什么帮助吗?

这是我的代码

NavigationAdapter

public class NavigationAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] NavigationTexts;
    private final String[] NavigationImages;

    public NavigationAdapter(Activity context, String[] NavigationTexts, String[] NavigationImages) {
        super(context, R.layout.navigation_layout, NavigationTexts);

        this.context=context;
        this.NavigationTexts=NavigationTexts;
        this.NavigationImages=NavigationImages;
    }

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

        TextView Name = (TextView) rowView.findViewById(R.id.NavigationText);
        ImageView ImgButton = (ImageView) rowView.findViewById(R.id.NavigationImage);


        Name.setText(NavigationTexts[position]);
        int resID = context.getResources().getIdentifier(NavigationImages[position], "drawable", getContext().getPackageName());
        ImgButton.setImageResource(resID);

        return rowView;

    }

}

和我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:id="@+id/NavigationImage"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:scaleType="fitXY" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/NavigationText"
        android:layout_alignBottom="@+id/NavigationImage"
        android:layout_marginBottom="56dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#ffffff"
        android:gravity="center" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您可以在customAdapter中手动设置它。由于您已经覆盖onCreateViewHolder方法并为列表项膨胀实际布局,您可以这样:

伪代码:

var listViewHeight = context.getResources().getDisplayMetrics().height / 4;
//if you have toolbar or some other component, take its height into calculation also.
View view = inflater.inflate(layoutId, container, false);
View listViewContainer = view.findViewById(containerId);
listViewContainer.getLayoutParams().height = listViewheight;

更新:

我建议使用RecyclerView而不是ListView和RecyclerView.Adapter而不是ArrayAdapter。但要回答你的问题:

与上述逻辑相同。

将id设置为父RelativeLayout为&#34; layoutContainer&#34;或其他东西,并在你的getView()方法中使用上面的行。