padding 与margin

来源:互联网 发布:手机怎么登网页淘宝 编辑:程序博客网 时间:2024/05/21 08:27

android:layout_margin就是设置view的上下左右边框的额外空间。

android:padding是设置内容相对view的边框的距离。

 

在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的。

在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的。

在AbsoluteLayout中,没有android:layout_margin属性。


maigin 是外边距

padding 是内边距 就像盒子的外边距和内边距


margin 是框架外的距离

padding 是框架中的距离


margin 表示的是view的左边缘与parentview的左边缘的距离

padding 指的是view中的content与view边缘的距离


margin 一般用来描述控件间位置关系
padding 一般描述控件内容和控件的位置关系

例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    >
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
 android:layout_marginBottom="10dip"
    />
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
  android:layout_marginBottom="10dip"
    />
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:text="@string/hello"
    android:paddingLeft="50dip"
    android:paddingRight="50dip"
    android:paddingTop="50dip"
    android:paddingBottom="50dip"
    android:layout_marginBottom="10dip"
    />
</LinearLayout>

0 0
原创粉丝点击