Andorid 布局layout_margin和padding分析

来源:互联网 发布:最花钱的游戏知乎 编辑:程序博客网 时间:2024/06/06 14:30

margin是控件或者布局的整体区域,相对于父布局以及周围控件和布局的上下左右的距离。

padding是当前控件或者布局的有效区域(比如下图中红色的文本输入框的输入区域),相对于控件或者布局的整体区域的边界的上下左右的距离。



布局文件如下:

看这个标识图则一目了然



  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginBottom="15dp"  
  11.         android:layout_marginLeft="50dp"  
  12.         android:layout_marginRight="50dp"  
  13.         android:orientation="horizontal" >  
  14.   
  15.         <TextView  
  16.             android:layout_width="0dp"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_weight="1"  
  19.             android:text="姓名"  
  20.             android:textSize="20sp" />  
  21.   
  22.         <EditText  
  23.             android:id="@+id/name"  
  24.             android:layout_width="0dp"  
  25.             android:layout_height="wrap_content"  
  26.             android:layout_weight="5"  
  27.             android:hint="请输入您的姓名" />  
  28.     </LinearLayout>  
  29.   
  30.     <LinearLayout  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:orientation="vertical"   
  34.         android:layout_marginTop="100dp"  
  35.         android:layout_marginLeft="30dp"  
  36.         android:layout_marginRight="30dp"  
  37.         android:layout_marginBottom="100dp"  
  38.           
  39.         >  
  40.   
  41.         <TextView  
  42.             android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_weight="5"  
  45.             android:text="年龄"  
  46.             android:textSize="18sp" />  
  47.         <View  
  48.             android:layout_width="match_parent"  
  49.             android:layout_height="2dp"  
  50.             android:background="@color/dividingline_color" />  
  51.   
  52.         <EditText  
  53.             android:id="@+id/age"  
  54.             android:layout_width="fill_parent"  
  55.             android:layout_height="wrap_content"  
  56.             android:layout_weight="1"  
  57.             android:paddingTop="30dp"  
  58.             android:paddingBottom="30dp"  
  59.             android:paddingLeft="30dp"  
  60.             android:paddingRight="30dp"  
  61.               
  62.             android:background="#aa0000"  
  63.             android:hint="请输入您的年龄" />  
  64.     </LinearLayout>  
  65.   
  66. </LinearLayout> 

0 0