zoukankan      html  css  js  c++  java
  • android:padding和android:margin的区别 详解

    转载请说明博客地址:http://blog.csdn.net/qq_32059827/article/details/51487997

    看了网上的类似博客,并没有给出确定的区别。现在具体分析一下padding和android:margin的区别

    首先看一张图:


    顾名思义。padding为内边距;margin为外边距。

    安卓的view是一块矩形区域,padding是内边距,就是view(里面的内容)永远都至少和边界有一段设定好的距离。margin是外边距,就是外面的view无法完全靠近这个view的边界,至少要间隔一段设置好的距离。

    我理解成:某个View指定为padding是针对该View里面的子View距离该View距离而言的。某个View指定为margin是针对该View本身距离别人或者父View而言的。

    再看一段代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp" >//这里的padding表示他的子view即下面的两个LinearLayout与此LinearLayout的距离是10dp
    
        <LinearLayout
            android:id="@+id/left_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:background="@drawable/message_left" >
    
            <TextView
                android:id="@+id/left_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp"
                android:textColor="#fff" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/right_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@drawable/message_right" >
    
            <TextView
                android:id="@+id/right_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="10dp" />//这代表TextView与它所在的父view即LinearLayout的距离为10dp
        </LinearLayout>
    
    </LinearLayout>

    通过测试,再在子LinearLayout里面加入padding和margin的确是和所写一致。


    同样地,再给出一个例子验证所述的正确性:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" 
        android:padding="30dp">//表示这个view里面的view即linerlayout与该view的边距为30dp
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" >//表示该linerlayout相对于本身与外面的view的边距为10dp
    
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="30dp"//表示此button相对于本身与外view即linerlayout和button2(可以直接理解成与四周的view)边距为30dp
                android:text="button1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"//表示此button相对于本身与左边的view即button1的边距为30dp
                android:text="button2" />
        </LinearLayout>
    
    </LinearLayout>

    图解如下:


    若有其他更好的理解,还望指正、指导。


  • 相关阅读:
    itest(爱测试) 紧急 BUG 修复版(4.5.6)发布,,开源BUG 跟踪管理 & 敏捷测试管理&极简项目管理软件
    itest(爱测试) 4.5.5 发布,开源BUG 跟踪管理 & 敏捷测试管理&极简项目管理软件
    itest(爱测试) 4.5.2 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    itest(爱测试) 4.5.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    itest(爱测试) 4.5.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    itest(爱测试) 4.4.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    itest(爱测试) 4.3.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    itest(爱测试) 4.3.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件
    热烈庆祝itest 入围2019最受欢迎国产开源软件
    Web渗透测试 之 文件上传、文件包含
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299776.html
Copyright © 2011-2022 走看看