【问题标题】:Android: How can you align a button at the bottom and listview above?Android:如何对齐底部的按钮和上方的列表视图?
【发布时间】:2011-06-23 14:53:48
【问题描述】:

我想在列表视图的底部有一个按钮。

如果我使用 relativeLayout/FrameLayout,它会对齐,但 listView 会下降到非常底部。

(在底部按钮的后面)

框架布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </FrameLayout>
</FrameLayout>

相对布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/btnButton"
            android:text="Hello"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom" />
    </RelativeLayout>
</RelativeLayout>

以上两个代码仅与第一个图像一样工作。我想要的是第二张图片。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: android listview layout alignment


    【解决方案1】:

    FrameLayouts 的目的是将事物相互叠加。这不是你想要的。

    在您的 RelativeLayout 示例中,您将 ListViews 的高度和宽度设置为 MATCH_PARENT 这将使其占用与其父级相同的空间量,从而占用页面(并覆盖按钮)。

    尝试类似:

    <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical">
      <ListView 
         android:layout_width="match_parent" 
         android:layout_height="0dip" 
         android:layout_weight="1"/>
      <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0"/>
    </LinearLayout>
    

    layout_weight 指示如何使用额外空间。 Button 不想超出它所需的空间,所以它的权重为 0。ListView 想要占用所有额外的空间,所以它的权重为 1。

    您可以使用RelativeLayout 完成类似的操作,但如果只是这两个项目,那么我认为LinearLayout 更简单。

    【讨论】:

    • 解决了我的问题,但android:weight 应该是:android:layout_weight
    • @Mayra 您的解决方案效果很好。但是,它是否只适用于 android:orientation="vertical" 而不适用于水平或未指定方向?
    • 但是这个解决方案并不能处理listview不够长,无法填满整个屏幕垂直空间的情况。如果列表视图仅占屏幕的一半,则该按钮会出现在列表视图的正下方,因此最终会出现在屏幕中间的某个位置。在这种情况下,我们怎样才能让按钮总是出现在底部呢?
    • android:layout_weight="0dip" 是不正确的,它应该是 android:layout_weight="0" 任何方式都是一个好方法
    • 这个答案比我在官方文档中遇到的任何东西都更好的解释,这一切似乎只是向已经知道它的人解释了一些东西。
    【解决方案2】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#ffffff"
      >
    
        <ListView android:id="@+id/ListView01" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="1">
        </ListView>
    
        <FrameLayout android:id="@+id/FrameLayout01" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content">
             <Button android:id="@+id/Button01" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:text="button" 
                  android:layout_gravity="center_horizontal">
            </Button>
        </FrameLayout>
    
      </LinearLayout>
    

    这是您正在寻找的设计。 试试看。

    【讨论】:

      【解决方案3】:

      我需要在底部并排放置两个按钮。我使用了水平线性布局,但为按钮的线性布局分配 android:layout_height="0dp"android:layout_weight="0" 不起作用。为按钮的线性布局分配android:layout_height="wrap_content"。这是我的工作布局:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >
      
          <ListView
              android:id="@+id/listView1"
              android:layout_width="match_parent"
              android:layout_height="0dp" 
              android:layout_weight="1" />
      
          <LinearLayout 
              android:layout_width="match_parent"
              android:layout_height="wrap_content" 
              android:orientation="horizontal">
      
              <Button
                  android:id="@+id/new_button"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="New" />
      
              <Button
                  android:id="@+id/suggest_button"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:text="Suggest" />
      
          </LinearLayout>
      
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        RelativeLayout 将忽略其子级 android:layout_widthandroid:layout_height 属性,如果子级具有正确定义其 leftrighttopbottom 值的属性。

        要实现右图的结果,在按钮上方显示列表,您的布局应如下所示:

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <android.support.v7.widget.RecyclerView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@android:id/button1"
                android:layout_alignParentTop="true"/>
        
            <Button
                android:id="@android:id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="@android:string/ok"/>
        
        </RelativeLayout>
        

        关键是在你的RecyclerView中定义android:layout_alignParentTop(定义top值)和android:layout_above(定义bottom值)。这样,RelativeLayout 将忽略 android:layout_height="match_parent"RecyclerView 将放置在 Button 上方。

        此外,如果您有更复杂的布局并且仍需要定义这些值,请务必查看 android:layout_alignWithParentIfMissing

        【讨论】:

          【解决方案5】:

          我使用的是 Xamarin Android,我的要求与上面的 William T. Mallard 完全相同,即下面有 2 个并排按钮的 ListView。 解决方案是这个答案在 Xamarin Studio 中不起作用 - 当我将 ListView 的高度设置为“0dp”时,ListView 就消失了。

          我的 Xamarin Android 代码如下:

          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          
          <ListView
              android:id="@+id/ListView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:layout_above="@+id/ButtonsLinearLayout" />
          <LinearLayout
              android:id="@id/ButtonsLinearLayout"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:orientation="horizontal"
              android:layout_alignParentBottom="true">
              <Button
                  android:id="@+id/Button1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
              <Button
                  android:id="@+id/Button2"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1" />
          </LinearLayout>
          </RelativeLayout>
          

          我将 ButtonsLinearLayout 对齐到屏幕底部,并将 ListView 设置在 ButtonsLinearLayout 上方。

          【讨论】:

            【解决方案6】:

            @jclova 你可以做的另一件事是在相对布局中使用layout-below=@+id/listviewid

            【讨论】:

              【解决方案7】:

              在列表视图的相对布局高度中,match_parent 是 fill_parent(适用于 2.1 和更早版本)所以最好的解决方案是如果你想使用相对布局,那么首先声明你的按钮然后声明你的列表视图,使列表视图位置如上您的按钮 ID,如果您希望按钮始终位于底部,则将其设置为 alignParentBottom .. 片段是

              <RelativeLayout 
              android:layout_width="fill_parent" android:layout_height="fill_parent"
              android:id="@+id/rl1"><Button 
               android:layout_width="MATCH_PARENT" 
               android:layout_height="WRAP_CONTENT" 
               /><ListView 
               android:layout_width="MATCH_PARENT" 
               android:layout_height="0" 
               android:layout_above="@id/listview"/></RelativeLayout>
              

              这可以防止您的列表视图占据整个位置并使您的按钮出现..

              【讨论】:

              • (警告:前面是次要的 Android 术语迂腐。)不推荐使用的术语是“fill_parent”,而不是“match_parent”。这对我来说很有意义,因为“填充”意味着两个维度,而“匹配”意味着“相同”。
              【解决方案8】:

              这将是解决问题的最佳和最简单的解决方案。只需在要相对于该布局移动的布局中添加 android:layout_above="@id/nameOfId"

              <?xml version="1.0" encoding="utf-8"?>
              <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  tools:context="com.sumeru.commons.activity.CommonDocumentUploadActivity">
              
                  <ListView
              
                      android:id="@+id/documentList"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_above="@id/verifyOtp" />
              
              
                  <com.sumeru.commons.helper.CustomButton
                      android:id="@+id/verifyOtp"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_alignParentBottom="true"
                      android:text="@string/otp_verification" />
              </RelativeLayout>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-02-23
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-06-10
                • 2020-10-04
                相关资源
                最近更新 更多