layout_gravity="bottom" 失效

2017-03-20  本文已影响0人  NathanXu

一、概述
在LinearLayout布局下,对Bottom添加属性 layout_gravity="bottom" 失效。[xhtml] view plain copy

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:<a href="http://lib.csdn.net/base/android" class='replace_word' title="Android知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
</Button>
</LinearLayout>

我觉得原因是LinearLayout 是竖直排列widgets的布局,所以竖直方向上有可能同时存在多个widgets。如果有两个或两个以上的widgets设置了android:layout_gravity="bottom"属性,那么系统就不能分辨出到底哪个widget放置在底部。所以在LinearLayout的vertical布局下,android:layout_gravity="bottom"无效。

二、解决方法
可以通过RelativeLayout布局android:layout_alignParentBottom="true"来实现放置底部效果。[xhtml] view plain copy

<?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"
>
<Button
android:id="@+id/ProcessBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Process">
</Button>
</RelativeLayout>

上一篇下一篇

猜你喜欢

热点阅读