【问题标题】:What am I misunderstanding about aligning views at the bottom?我对在底部对齐视图有什么误解?
【发布时间】:2014-08-17 00:42:39
【问题描述】:

我试图将我的线性布局对齐到整个布局的底部,这是相对的,并在这里找到了解决方案:How to align views at the bottom of the screen?

但是为什么这也行不通呢?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sandbox.activities.ListActivity">

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

<LinearLayout
    android:id="@+id/search_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/my_listview"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true">

    <EditText
        android:id="@+id/my_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Enter Search Term" />

    <Button
        android:id="@+id/find_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Find"
        android:onClick="findThings"/>

</LinearLayout>

如果列表视图很短,比如 1 行,“search_footer”就在它的下方,并且它下方有一个巨大的间隙。

1) "search_footer" 被告知在父级的底部对齐,这是相对布局。既然relativelayout的高度是match_parent,而且是根布局,就是屏幕的整个高度,那为什么不管listview大小,“search_footer”都不在屏幕底部呢?

2) 另外,如果我将 search_footer 的高度更改为 match_parent,我希望按钮非常高,但它保持不变——为什么?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    添加到ListView这个属性

    android:layout_above="@id/search_footer"
    

    并删除

    android:layout_below="@id/my_listview"
    

    来自LinearLayout的属性。

    【讨论】:

      【解决方案2】:

      您指定search_footer 与父级底部对齐,但也位于列表视图下方,因此在列表较短的情况下,搜索页脚只会在列表视图和屏幕底部之间的空间中延伸。您想指定页脚与父级底部对齐,列表视图位于其上方:

      <ListView
          android:id="@+id/my_listview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          android:layout_above="@+id/search_footer"
          />
      
      <LinearLayout
          android:id="@+id/search_footer"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          android:layout_alignParentBottom="true">
      

      当您更改search_footer 的高度时,按钮不会更改,因为它们设置为android:layout_height="wrap_content"。当你这样做时,你的布局通常会改变,但在这种情况下,由于与上述相同的原因,它也没有改变

      【讨论】:

      • 使用 android:layout_below="@id/my_listview" 他会在 xml 文件中得到 Circular dependencies 错误。
      • @Onik 我认为 OrBar 出现了复制粘贴错误,忘记删除对列表视图的引用。
      • 关于页脚高度,听起来你说的是更深层次的嵌套布局设置会覆盖它们的父级?虽然我看到您的解决方案适用于页脚,但我仍然不明白为什么说页脚位于列表视图下方而列表视图位于页脚上方是不同的;在这两种情况下,父级都是相对布局,其高度是通过 match_parent 的全屏
      • @HukeLau_DABA 以下只是我的想法。用 match_parent attr 说父母的高度是全屏的高度并不是一个可靠的规则。如果元素不适合高度怎么办?图形预览如何提前计算元素的高度?我想,这就是你得到的结果的重点。当“说页脚低于列表视图”时如何计算列表的高度?但是当“说 listview 在页脚之上”时,你确切地知道它的高度在屏幕顶部和页脚之间......
      • @Onik,是的,这是一个复制粘贴错误。页脚低于列表视图和列表视图高于页脚之间的区别归结为两者中的哪一个首先对齐,哪一个将基于另一个对齐。基本上,您希望更重要的(布局明智)锚定到底部,并拉伸列表视图以适应其上方的空间,而不是让列表视图占用所需的空间,并拉伸页脚以适应区域打击它。
      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多