控件的margin属性是用来控制控件之间的间距,那么当两个控件需要重叠时该如何进行布局呢?
最常用的做法是使用FrameLayout帧布局,但是帧布局不能像LinearLayout能设置比重layout_weight灵活。
如果要使用LinearLayout布局的灵活性,又要使控件进行重叠,这时就可以使用margin的负值来进行处理重叠。
例子:
布局:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools= "http://schemas.android.com/tools"
-
android:layout_width= "fill_parent"
-
android:layout_height= "fill_parent"
-
android:orientation= "vertical"
-
tools:context= ".MainActivity" >
-
-
<LinearLayout
-
android:layout_width= "fill_parent"
-
android:layout_height= "wrap_content"
-
android:layout_marginLeft= "10dp"
-
android:layout_marginRight= "10dp"
-
android:layout_marginTop= "10dp"
-
android:orientation= "horizontal" >
-
-
<TextView
-
android:layout_width= "fill_parent"
-
android:layout_height= "wrap_content"
-
android:layout_marginRight= "-4dp"
-
android:layout_weight= "1"
-
android:background= "@drawable/register_bar_left_green"
-
android:gravity= "center"
-
android:text= "验证手机号码"
-
android:textSize= "12sp" />
-
-
<TextView
-
android:layout_width= "fill_parent"
-
android:layout_height= "wrap_content"
-
android:layout_marginLeft= "-4dp"
-
android:layout_marginRight= "-4dp"
-
android:layout_weight= "1.1"
-
android:background= "@drawable/register_bar_mid_green"
-
android:gravity= "center"
-
android:text= "设置密码"
-
android:textSize= "12sp" />
-
-
<TextView
-
android:layout_width= "fill_parent"
-
android:layout_height= "wrap_content"
-
android:layout_marginLeft= "-4dp"
-
android:layout_marginRight= "-4dp"
-
android:layout_weight= "1.1"
-
android:background= "@drawable/register_bar_mid_green"
-
android:gravity= "center"
-
android:text= "填写信息"
-
android:textSize= "12sp" />
-
-
<TextView
-
android:layout_width= "fill_parent"
-
android:layout_height= "wrap_content"
-
android:layout_marginLeft= "-4dp"
-
android:layout_weight= "1.2"
-
android:background= "@drawable/register_bar_right_green"
-
android:gravity= "center"
-
android:text= "完成"
-
android:textSize= "12sp" />
-
</LinearLayout>
-
-
</LinearLayout>
原文链接:https://blog.csdn.net/alan_biao/article/details/17218069