android - 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>
我不能使用片段等其他视图,因为那样我的代码会有很多变化。
解决方案
您将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>
推荐阅读
- php - Laravel Websockets Error in connection Error during WebSocket handshake: Unexpected response code: 404
- flutter - 更改一个 Provider 中的属性会将另一个 Provider 中的属性更改为 List Flutter
- c# - 可空引用类型允许使用 AllowNullAttribute 为空
- teamcity - 如何获取每个构建步骤的持续时间
- node.js - 如何在 nginx.conf 文件中为 Last-Modified 标头格式化 $date_gmt
- vue.js - 如何让我的 vue 路由器进入视图顶部?
- .net - 运行 Windows 批处理命令时 Jenkins 构建错误
- image - 我应该禁用图像的 Content-Security-Policy 标头吗?
- api - Github action cronjob curl POST 并不总是有效
- javascript - 最小值和最大值 Reactstrap 输入类型日期(日历)