首页 > 解决方案 > android中对话框中ListView的高度未根据ListView中的项目数设置;results in blank space when there are less number of options

问题描述

当有足够的选项时,结果是合适的;image-> 结果没问题,但是当listview中的选项少时,就会有空白空间;我不想在那里。图像-> 不好的结果

I want to set the height like, when there are less options, there should not be any blank space bw. 取消按钮和列表视图。

代码list_view.xml

<?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">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:fontFamily="@font/quicksand_regular"
        android:text="Please select country"
        android:textAlignment="center"
        android:textSize="14sp" />


    <ListView
        android:id="@+id/dialogList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:background="@drawable/listview_background"
        android:choiceMode="singleChoice"></ListView>


    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/button_background"
        android:fontFamily="@font/quicksand_bold"
        android:gravity="center_horizontal"
        android:paddingLeft="50dp"
        android:paddingRight="50dp"
        android:text="cancel"
        android:textColor="#fff" />

</LinearLayout>
</RelativeLayout>

代码list_item.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">

<com.rohan.myvoice.CustomTextView
    android:id="@+id/textViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="Text"
    android:textAlignment="center"
    android:textColor="@drawable/option_select_color"
    android:textSize="16sp" />


<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:background="@color/line_light" />

</LinearLayout>

我不能使用片段等其他视图,因为那样我的代码会有很多变化。

标签: androidlistviewdialogpopupheight

解决方案


您将layout_weight 设置为 10,这对于列表视图来说不是必需的,我们只想在 layout_height 中使用 wrap_content:-

 <ListView
        android:id="@+id/dialogList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:background="@drawable/listview_background"
        android:choiceMode="singleChoice"></ListView>

请使用我的代码通过 Trying Recycler 视图而不是列表视图进行检查:-

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

    <TextView
        android:id="@+id/title_textView"
        android:layout_width="250dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:layout_marginBottom="10dp"
        android:text="Please select country"
        android:textAlignment="center"
        android:textSize="14sp" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/dialogList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice" />


    <Button
        android:id="@+id/cancel_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/button_background"
        android:gravity="center"
        android:text="cancel"
        android:textColor="#fff" />

</LinearLayout>

推荐阅读