Android listview项目高度fill_parent不起作用

标签 android listview height fill

我有一个带有包含 ListView 的 mainactivity 的 Android 应用程序,这是主 Activity 布局的代码:

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

    <ListView  android:id="@+id/commandList" 
               android:layout_width="fill_parent"
               android:layout_height="0dp"
               android:layout_weight="1"
              />

</LinearLayout>

在 ListView 中我显示了部分 View ,这是 View 布局的代码:

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

    <TextView
        android:id="@+id/commandText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

</LinearLayout>

我希望元素能够填充 mainactivitylayout 的高度,但结果是这样的: result image enter image description here

谢谢你的问候

最佳答案

Waqas 就在上面的评论中。 (这完全扼杀了使用ListView的目的)

但是你仍然想在适配器的 getView() 中实现这个,尝试下面的代码

convertView.getLayoutParams().height = parent.getHeight() / NO_OF_ITEMS;
convertView.requestLayout();

关于Android listview项目高度fill_parent不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20990071/

相关文章:

android - 在 Android 版 Chrome 中通过 Google ID 自动登录

android - 如何将数据从 Activity 发送到其父 fragment ?

android - 自定义 Listview - 我在哪里设置我的点击处理程序

html - 当我使用 jquery 计算 body 的高度时,绝对位置元素的高度不会被包括在内吗?

ios - 静态更改文本字段的高度时出现意外行为

Android 应用在首次发布时在 Google Play 商店中被暂停

java - 如何通过gcm发送自定义消息并得到其响应?

ios - React Native ListView 具有可更新的固定部分标题

html - 如何使以百分比设置的 div 高度随其内容扩展?

java - 试图从日历中获取日期...应用程序崩溃