【问题标题】:Height of ListView in dialog in android not set as per the number of items in ListView; results in blank space when there are less number of optionsandroid中对话框中ListView的高度未根据ListView中的项目数设置;在较少数量的选项时导致空白空间
【发布时间】:2019-06-21 07:28:58
【问题描述】:

当有足够的选项时,结果是合适的; image-> result is ok,但是当列表视图中的选项较少时,就会有空白空间;我不想在那里。image-> not ok result

我想设置高度,如较少的选项,不应该有任何空白空间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>

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

【问题讨论】:

    标签: android listview dialog popup height


    【解决方案1】:

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

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

    请使用我的代码通过尝试 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>
    

    【讨论】:

    • 如果我不提供重量 10 那么,取消按钮将在列表视图下。!
    • 您可以通过设置按钮的特定宽度高度来尝试
    • 请尝试更改答案,如果有帮助请告诉我
    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多