Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解

2019-12-10 19:40:45于丽

再在WVGA800的模拟器下看看显示效果:

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解

Tip: 在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。

二、相对布局RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:

XML/HTML代码:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#fff" 
xmlns:android="http://www.easck.com/apk/res/android"><ImageView android:id="@+id/ImageView01" 
android:src="@drawable/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="40dip" 
> 
</ImageView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView01" 
android:text="Android2.2 学习指南" 
android:textColor="#0f0" 
android:textSize="28dip" 
android:layout_below="@id/ImageView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="10dip"> 
</TextView> 
<TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/TextView02" 
android:text="图文并茂,理论清晰,操作性强" 
android:textColor="#333" 
android:textSize="18dip" 
android:layout_below="@id/TextView01" 
android:layout_centerHorizontal="true" 
android:layout_marginTop="5dip“> 
</TextView> 
</RelativeLayout>