简体   繁体   English

如何调整ListView中的行高,使其占据剩余空间?

[英]How can I adjust the row heights in a ListView so they take up the remaining space?

I have a ListView which can have 2-4 rows in it. 我有一个ListView,其中可以包含2-4行。 I want the rows to change their height so they are even, and fill up the remaining space in the LinearLayout. 我希望这些行更改其高度,使它们均匀,并填充LinearLayout中的剩余空间。

This is a snippet of what I have: 这是我所拥有的片段:

activity_layout.xml: activity_layout.xml:

<LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:layout_alignParentRight="true"
 android:layout_alignParentEnd="true"
 android:layout_alignParentLeft="true"
 android:layout_alignParentStart="true"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:paddingBottom="@dimen/activity_vertical_margin">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:background="@drawable/quiz_question_box"
            android:textColor="#000000"
            android:textAlignment="center"
            android:gravity="center_vertical"
            android:textSize="25sp"
            android:id="@+id/question_text_view"/>

        <ListView
            android:id="@+id/options_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"/>
</LinearLayout>

row_layout.xml: row_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:padding="0dp">

<TextView
    android:id="@+id/row_content_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:padding="10dp"
    android:textColor="#FFFFFF"
    android:gravity="center_vertical"
    android:textSize="20sp">
</TextView>

</LinearLayout>

Gives me this: 给我这个:

在此处输入图片说明

I've tried setting the weight of pretty much everything I can think of, including the LinearLayout in the row_layout.xml with the hope that the rows would be distributed evenly, but no luck. 我尝试设置几乎所有可以想到的东西的权重,包括row_layout.xml中的LinearLayout,希望行将平均分配,但没有运气。 I've also tried setting the parameters of the view in the getView method of my Adapter: 我也尝试在Adapter的getView方法中设置视图的参数:

    @Override
public View getView(int position, View convertView, ViewGroup parent){
    View view = super.getView(position, convertView, parent);
    view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.5f));
    if((position % 2 == 1) && (position != getNumberOfItemsInList())){
        view.setBackgroundResource(R.drawable.quiz_middle_answer_bordered_light);
    } else if ((position % 2 == 1) && (position == getNumberOfItemsInList()))  {
        view.setBackgroundResource(R.drawable.quiz_bottom_answer_rounded_light);
    } else if ((position % 2 == 0) && (position != getNumberOfItemsInList())){
        view.setBackgroundResource(R.drawable.quiz_middle_answer_bordered_dark);
    } else {
        view.setBackgroundResource(R.drawable.quiz_bottom_answer_rounded_dark);
    }
    return  view;
}

But that hasn't worked either. 但这也不起作用。 How can I achieve this? 我该如何实现?

In your text views you declared the height to be wrap context that means they'll take the minnimal height and that's normally what you want to do. 在您的文本视图中,您声明了高度为自动换行上下文,这意味着它们将采用最小高度,这通常是您要执行的操作。 However you want them to fill the entire space so what you should do is to set the height to match parent. 但是,您希望它们填充整个空间,因此您应该设置高度以匹配父对象。 Now to prevent one of them to occupy the full object and therefore pushing the other off the screen you need to assign android:weight to them 现在,为了防止其中一个占据整个对象,从而将另一个推离屏幕,您需要为它们分配android:weight

Here's what you can do, 这就是你可以做的

1) Find the screen height in dp 1)在dp中找到屏幕高度

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();    
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;

find half the screen height and pass this value to the list adapter 找到一半的屏幕高度,并将此值传递给列表适配器

2) Divide this value by item count. 2)将该值除以项目计数。 You can call the method getItemCount() 您可以调用方法getItemCount()

Now we have the height of each list view child. 现在我们有了每个列表视图子级的高度。 Now on getView() you can the height of linear layout with this value. 现在在getView()您可以使用此值来线性布局的高度。

将weigth分配给行,这样它们就可以独立于设备屏幕占用任意大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM