Android margin的负值的使用

来源:互联网 发布:微观调查数据 编辑:程序博客网 时间:2024/06/05 05:06

控件的margin属性是用来控制控件之间的间距,那么当两个控件需要重叠时该如何进行布局呢?

最常用的做法是使用FrameLayout帧布局,但是帧布局不能像LinearLayout能设置比重layout_weight灵活。

如果要使用LinearLayout布局的灵活性,又要使控件进行重叠,这时就可以使用margin的负值来进行处理重叠。

例子:



布局:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginLeft="10dp"  
  12.         android:layout_marginRight="10dp"  
  13.         android:layout_marginTop="10dp"  
  14.         android:orientation="horizontal" >  
  15.   
  16.         <TextView  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_marginRight="-4dp"  
  20.             android:layout_weight="1"  
  21.             android:background="@drawable/register_bar_left_green"  
  22.             android:gravity="center"  
  23.             android:text="验证手机号码"  
  24.             android:textSize="12sp" />  
  25.   
  26.         <TextView  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:layout_marginLeft="-4dp"  
  30.             android:layout_marginRight="-4dp"  
  31.             android:layout_weight="1.1"  
  32.             android:background="@drawable/register_bar_mid_green"  
  33.             android:gravity="center"  
  34.             android:text="设置密码"  
  35.             android:textSize="12sp" />  
  36.   
  37.         <TextView  
  38.             android:layout_width="fill_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:layout_marginLeft="-4dp"  
  41.             android:layout_marginRight="-4dp"  
  42.             android:layout_weight="1.1"  
  43.             android:background="@drawable/register_bar_mid_green"  
  44.             android:gravity="center"  
  45.             android:text="填写信息"  
  46.             android:textSize="12sp" />  
  47.   
  48.         <TextView  
  49.             android:layout_width="fill_parent"  
  50.             android:layout_height="wrap_content"  
  51.             android:layout_marginLeft="-4dp"  
  52.             android:layout_weight="1.2"  
  53.             android:background="@drawable/register_bar_right_green"  
  54.             android:gravity="center"  
  55.             android:text="完成"  
  56.             android:textSize="12sp" />  
  57.     </LinearLayout>  
  58.   
  59. </LinearLayout>  
0 0