相对布局:根据所设置的参照控件来进行布局,设置的参考控件可以是父控件也可以是其他子控件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间的按钮,很长很长很长" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button1" 控件的相对位置
android:layout_alignLeft="@id/button1"
android:text="上面的按钮" >
</Button>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/button1"
android:layout_below="@id/button1"
android:text="下面的按钮" >
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间的按钮,很长很长很长" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button1" 控件的相对位置
android:layout_alignLeft="@id/button1"
android:text="上面的按钮" >
</Button>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/button1"
android:layout_below="@id/button1"
android:text="下面的按钮" >
</Button>
</RelativeLayout>
本文深入解析了使用RelativeLayout进行Android布局的方法,通过设置参照控件,实现子控件的精确相对定位。具体展示了如何通过android:layout_centerInParent、android:layout_above、android:layout_alignLeft等属性,将多个按钮分别设置为居中、上方及右侧的位置,以达到美观且功能性的布局效果。
2393

被折叠的 条评论
为什么被折叠?



