Android基础之layout_margin 和padding的区别

最近学的有点晕连layout_margin和padding都给忘了,这次记录一下。

分析:下图为测试效果,从上至下为 普通 -> 含padding ->含layout_margin,绿色为布局的填充背景

假若在没有背景的前提下,设置线性布局的padding和margin效果是相近的。

当为padding时指代 当前布局先布局然后再扩充,视觉上的效果是设置内容距离边线的距离,一般来说padding是扩充控件用的,例如给一些小的控制设置padding让其可操作范围变大,一般不会给布局设置padding

而为margin时指代当前布局先布局然后再调整与相邻视图的距离,视觉上的效果是设置整体距离边线的距离,一般给布局设置margin来造成匀称的感觉


结果:


xml代码:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:background="@color/green1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:background="@color/green1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15dp">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:background="@color/green1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:orientation="horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />

            <Button
                android:padding="40dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="你好" />


        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

    </LinearLayout>

猜你喜欢

转载自blog.csdn.net/crazyzhangxl/article/details/80085021