【问题标题】:Adding a listView between two elements在两个元素之间添加 listView
【发布时间】:2014-03-24 16:00:41
【问题描述】:

我想在顶部和底部按钮之间添加一个 listView。我不希望它有固定的大小,所以我尝试这样设置:

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

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button1" />

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

但是以这种方式设置布局文件时,列表视图当然在第一个按钮下方,但从那里向下到屏幕的按钮,而不是在按钮 2 上方停止。 --> 列表视图与按钮2重叠。

如何在这两个按钮之间放置列表视图?

【问题讨论】:

标签: android xml listview android-listview


【解决方案1】:

android:layout_above="@+id/button2" 属性添加到ListView 如下...这将使ListView 保持在button2 之上。

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

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/button1"
    android:layout_above="@+id/button2" />

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

【讨论】:

  • 这个解决方案非常好,但如果你真的不需要外部RelativeLayout来做其他任何事情,那么高度为0且重量为0的LinearLayout版本会更有效。
【解决方案2】:

类似的东西应该可以完成这项工作

<LinearLayout  android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical>
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:weight="1"/>

     <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>

【讨论】:

    【解决方案3】:

    试试这个..

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
     <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    </LinearLayout>
    

    或者

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    <Button 
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />
    
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button2"
        android:layout_below="@+id/button1" />
    
     <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      试试这个方法:

      <?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/button1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:text="Button" />
      
          <ListView
              android:id="@+id/listView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_below="@+id/button1"
              android:layout_above="@+id/button2" >
          </ListView>
      
          <Button
              android:id="@+id/button2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:text="Button" />
      
      </RelativeLayout>
      

      这完全符合您的要求。这很简单...:)

      【讨论】:

        【解决方案5】:

        您可以使用RelativeLayout,对于ListView,您可以通过参数layout_abovelayout_below 设置位置。

        【讨论】:

          【解决方案6】:

          尝试将您的 xml 插入到 RelativeLayout。

          <RelativeLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
          
          <Button 
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true" />
          
          <ListView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_below="@+id/button1" />
          
           <Button 
              android:id="@+id/button2
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true" />
          
          </RelativeLayout>
          

          【讨论】:

            【解决方案7】:

            将此属性设置为您的列表视图

            <ListView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/button1"
                android:layout_above="@+id/button2" />
            

            【讨论】:

              猜你喜欢
              • 2020-08-12
              • 2018-03-18
              • 2011-07-29
              • 1970-01-01
              • 2013-09-05
              • 2012-01-23
              • 2017-07-12
              • 1970-01-01
              • 2022-10-04
              相关资源
              最近更新 更多