1. 程式人生 > 其它 >Android強化——對Margin與Padding理解

Android強化——對Margin與Padding理解

LinearLayout下

  margin:外邊擴大距離,外邊為原。

  padding:內邊擴大距離,內邊為原。

  穿邊要負margin

  被遮擋調整ViewGroup大小,讓內部居中

  

如:1. textView對齊左邊時,marginLeft="20dp"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height
="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="123" android:layout_marginLeft="20dp" android:background="@drawable/shape_text"/> </LinearLayout>

 

 

如:2. textView居中時,marginLeft=“100dp”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width
="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:text="123" android:layout_marginLeft="100dp" android:background="@drawable/shape_text"/> </LinearLayout>

 

 

 

2. 穿邊要負margin

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="123"
        android:background="@drawable/shape_text"
        android:gravity="center"/>

    <TextView
        android:padding="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:layout_marginLeft="-10dp"
        android:background="@drawable/shape_text"/>

</LinearLayout>