0

我应该进行哪些更改以将相对布局置于底部?

      <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="86dp"
    android:layout_alignParentBottom="true"
    android:layout_weight="0.08"
    android:gravity="bottom" >
   </RelativeLayout>
      </LinearLayout>
4

4 回答 4

1

您需要将您的父 Layout 更改为 a RelativeLayout,因为该属性对孩子android:layout_alignParentBottom没有任何作用:LinearLayout

<?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" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:layout_alignParentBottom="true" >
    </RelativeLayout>
</RelativeLayout>
于 2013-08-23T19:32:09.277 回答
0

您可以通过使用 aFrameLayout而不是 a来实现这一点,LinearLayout因为默认情况下它会从上到下堆叠孩子。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp"
        android:layout_gravity="bottom" >
    </RelativeLayout>

</FrameLayout>
于 2013-08-23T19:31:39.983 回答
0

采用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- replace this by any other layout or view. You might want something here, right? -->    
    <View android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="86dp">
    </RelativeLayout>

</LinearLayout>
于 2013-08-23T19:42:23.773 回答
0

这里有两个问题。第一个是

android:gravity

是错的。这设置了gravitythisView的子项的内容。因此,例如在 a 中使用TextViewtext,如果TextView. 你想要的是

android:layout_gravity

这将Views设置gravity在其父级中。

第二个问题是它 android:gravity="bottom"不像你在LinearLayoutwho's orientationis中所期望的那样工作vertical。我不确定我能不能很好地解释为什么,所以我会尝试找到一个解释它的链接。但是,如果您将 the 更改为orientationLinearLayouthorizontal将看到这一点。

因此,您最好的选择是更改orientation是否有可能,如果没有,则将根更改ViewGroup为 a并按照某人已经建议的方式RelativeLayout使用该属性。android:alignParentBottom="true"

编辑解释

这篇博文这个 SO answer等等,稍微解释一下。但基本上,如果orientation是,horizontal那么Views 会按照我们的预期从左到右排列,因此您无法控制 left 和 right gravity。对于 a vertical orientation, top 和 bottom 也是如此,gravity因为它们垂直放置的位置已经由 Android 在设置时确定orientation。我希望这是有道理的。

于 2013-08-23T19:55:45.420 回答