天天看點

xml布局内容總結(二)--Android

關于xml中margin和padding的内容,在剛接觸xml的時候會有些混淆和模糊,在此進行總結,希望能幫助到有需要的人。

2.xml中margin和padding

<?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:layout_margin="10dp"                              //當上下左右四個方向的邊距一緻時,可以使用這個統一設定

    android:background="@color/red"

    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@color/yellow"

        android:layout_marginLeft="5dp"                       //當上下左右四個方向的邊距不一緻時,可以分别進行設定

        android:layout_marginTop="15dp"

        android:layout_marginRight="25dp"

        android:layout_marginBottom="35dp"

        android:orientation="vertical" >

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:layout_margin="15dp"                                  //這是外邊框

            android:padding="35dp"                                             //這是内邊框,内邊框的四個方向一緻時,可以統一設定

            android:background="@color/blue"

            android:orientation="vertical" >

            <LinearLayout

           android:layout_width="match_parent"

           android:layout_height="match_parent"

           android:background="@color/gray"

           android:paddingLeft="5dp"                              //内邊框的四個方向不一緻時,可以分别進行設定

           android:paddingTop="15dp"

           android:paddingRight="25dp"

           android:paddingBottom="35dp"

           android:orientation="vertical" >

                <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:background="@color/green"

            android:orientation="vertical" >

        </LinearLayout>

        </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

效果圖如下:

xml布局内容總結(二)--Android

通過效果圖和xml中的設定應該很快可以掌握margin和padding的使用