Android 如何在相对布局的底部放置一个相对布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3492721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:27:37  来源:igfitidea点击:

How to place a RelativeLayout at the bottom of a RelativeLayout?

androidandroid-linearlayoutandroid-relativelayout

提问by Upvote

I have a RelativeLayout, and this Layout has two childs, one is a MapViewand the other a RelativeLayoutthat contains a button.

我有一个RelativeLayout,这个 Layout 有两个孩子,一个是 a MapView,另一个 aRelativeLayout包含一个按钮。

I want it to look like that

我希望它看起来像那样

but my transparent box (a RelativeLayout) is always displayed at the top of the map.

但是我的透明框 (a RelativeLayout) 总是显示在地图的顶部。

<RelativeLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.google.android.maps.MapView
    android:id="@+id/mapView"/>

    <test.project.TransparentPanel 
      android:layout_width="fill_parent"
      android:layout_width="fill_parent">   

        <Button 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click Me!"/>   

    </test.project.TransparentPanel>

</RelativeLayout>   

(i've left out some things in the code)

(我在代码中遗漏了一些东西)

回答by Konstantin Burov

Try adding a alignParentBottom option to the transparent panel.

尝试向透明面板添加 alignParentBottom 选项。

<RelativeLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.google.android.maps.MapView 
    android:id="@+id/mapView"/>

  <test.project.TransparentPanel
    android:layout_width="fill_parent"
    android:layout_width="fill_parent"
    android:layout_alignParentBottom="true">

      <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Click Me!"/>         

  </test.project.TransparentPanel>

</RelativeLayout> 

回答by Janusz

As Konstantin pointed out use layout_alignParentBottomto position the button at the bottom of your view. The problem now is that the mapview will also stretch itself out to the bottom of the parent. Therefore the mapview will ′grow′ under the button until the parent is filled.

正如康斯坦丁指出的那样,用于layout_alignParentBottom将按钮定位在视图的底部。现在的问题是 mapview 也会延伸到父视图的底部。因此,地图视图将在按钮下“增长”,直到父级被填充。

Try the following. First position the button at the bottom of the parent view, then align above the button.

请尝试以下操作。首先将按钮定位在父视图的底部,然后在按钮上方对齐。

<RelativeLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <test.project.TransparentPanel
    android:id="@+id/button_area"
    android:layout_width="fill_parent"
    android:layout_width="fill_parent"
    android:layout_alignParentBottom="true">

      <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Click Me!"/>         

  </test.project.TransparentPanel>

  <com.google.android.maps.MapView 
    android:id="@+id/mapView"
    layout_above="@id/button_area"/>

</RelativeLayout>