margin和padding 的區別

2021-06-05 23:53:09 字數 1743 閱讀 9739

經常會看到這樣的布局android:layout_margintop="-1.0dip",比較迷惑。查了下資料,有篇文章講的不錯,拿來一起分享!

就拿layout_margintop和paddingtop為例。

layout_margintop是從當前設定的控制項的頭部,開始向上移動,直到碰到上乙個控制項/父容器的頂部,所經過的距離。為什麼這邊要用「或」呢,因為在不同的layout的效果是不一樣的。在linearlayout中,已經為內容控制項規定了是按順序排列的,所以layout_margintop的距離是指離上乙個控制項的底部的距離(垂直線性布局),但是在 relativelayout中,控制項可以選擇性堆疊在一起,所以,在relativelayout中的控制項之間沒有位置上的關係的話,那當前控制項的 layout_margintop就是指到父容器頂部的距離。

paddingtop是指從父容器的頂部觸發,向下移動,碰到的第乙個控制項的頂部的距離。這個還是很好理解的。

當父容器設定了paddingtop,子元素又設定了layout_margintop了的話,那兩者的距離就是兩個值的相加了。

layout_margintop那一段可能說的有點繞,發點**上來,大家試試就能理解了。

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:paddingtop="50dp" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button2"

android:layout_margintop="20dp"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:layout_margintop="20dp"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button3"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="button4"

android:layout_margintop="20dp"/>

示例圖:

margin和padding之我見

採用div css排版的話,如果有float屬性,在ie中選中文字的時候,經常會出現把一整個div全部選中,非常難看 而有時候又不會出現這種情況,自己是不是遇到過了?呵呵,奇怪吧,我來給你講.其實原因很簡單,主要看你做事是不是細心.這個就是margin和padding的區別了.如果你不知道margi...

margin和padding的區別

padding 10px 0 10px 20px,上10 右0 下10 左20 margin 10px 0 10px 20px,上10 右0 下10 左20 四個值順時針旋轉 目前web2.0已經越來被人們認可,因為喜歡搞web開發的人員不得不硬著頭皮去學習web2.0的標準,其中很重要的一條就是新...

margin和padding的區別

目前web2.0已經越來被人們認可,因為喜歡搞web開發的人員不得不硬著頭皮去學習web2.0的標準,其中很重要的一條就是新的布局規則,div css。以前基本上是用table布局的,這種傳統的方式簡單直觀,但是這新的標準我學習起來時常找不到北。呵呵,就拿今天這個例子來說吧,很多朋友肯定也有我這種情...