简体   繁体   English

如何在Android中将列表视图拉伸到全屏

[英]How to stretch a list view to the full screen in Android

Supose I have a layout oriented vertically with one button in the top, a list view in the middle, and a button in the bottom: 假设我有一个垂直定向的布局,顶部有一个按钮,中间有一个列表视图,底部有一个按钮:

------
BUTTON
LISTVIEW
LISTVIEW
LISTVIEW
------

I would like to make the whole layout scrollable, to be able to see the full list when scrolling down it: 我想使整个布局可滚动,以便在向下滚动时可以看到完整列表:

------
LISTVIEW
LISTVIEW
LISTVIEW
BUTTON
------

I think I found it, here's a solution for you, without using ScrollView (which is actually not needed): 我想我找到了,这是为您提供的解决方案,无需使用ScrollView (实际上不需要):

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

<Button android:id="@+id/btn1" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

<ListView android:id="@android:id/list1"
          android:layout_height="0dip"
          android:layout_width="match_parent"
          android:layout_weight="1" />

<ListView android:id="@android:id/list2"
          android:layout_height="0dip"
          android:layout_width="match_parent"
          android:layout_weight="1" />

<ListView android:id="@android:id/list3"
          android:layout_height="0dip"
          android:layout_width="match_parent"
          android:layout_weight="1" />

<Button android:id="@+id/btn2" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

您可以在ListView的标题中添加按钮,并在ListView的底部添加按钮。

  1. Add a Button as header and a Button as footer to the ListView. 将一个Button作为页眉和一个Button作为页脚添加到ListView。

  2. Write a custom Adapter for the ListView, return Button view at 0 and last position in getView(). 为ListView编写一个自定义的Adapter,在0处返回Button视图,并在getView()中返回最后一个位置。

I'm not really sure if you want to be able to scroll the buttons too, depending on that there are two ways which you can try. 我不确定您是否也可以滚动按钮,具体取决于您可以尝试两种方式。 First where the button won't change their position while list view is scrolling. 首先,滚动列表视图时按钮不会改变其位置。 In this situation your xml should look like this : 在这种情况下,您的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" >

    <Button
        android:id="@+id/top_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <ListView
        android:id="@+id/my_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_button"
        android:layout_below="@+id/top_button" />

    <Button
        android:id="@+id/bottom_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

The second option is when you want these two buttons to scroll while list view is scrolling too. 第二个选项是当您希望这两个按钮也在列表视图也在滚动时滚动。 To achieve this you should add Button s to your list view as header and footer : 为此,您应该将Button作为页眉和页脚添加到列表视图中:

mListView.addHeader(myFirstButton);
mListView.addFooter(mySecondButton);

And that should do the trick for you in both scenarios. 这在两种情况下都可以为您解决问题。

您应该使用android:layout_height="wrap_content"

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

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