Relative Layout 不僅可以指定同級的元素之間的位置關系(e.g. layout_toLeftOf) 還可以指定子元素與父元素之間的位置關系(e.g. layout_alignParentLeft 相對於父元素左對齊) 利用這一點,可以輕松實現圖片在屏幕底部對齊顯示。
代碼如下:
<?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"
android:divider="@drawable/line"
android:showDividers="middle">
<!--新聞內容 -->
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</WebView>
<!--底部圖片-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"<!--在父元素中左對齊 -->
android:background="@drawable/back" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"<!--在父元素中居中對齊 -->
android:background="@drawable/house" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/share"
android:layout_alignParentRight="true" <!--在父元素中右對齊 -->
android:layout_marginRight="10dp"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
</LinearLayout>