Thursday, February 21, 2019

Margin and Padding

In xml file, Margin and Padding are different.
For example, we have 2 textView side by side.
<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/line2"
            android:text="Example text"
            android:textColor="#800000"
            android:textSize="16sp" />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/line2"
            android:text="Example text"
            android:textColor="#800000"
            android:textSize="16sp" />
    </LinearLayout>
Add marginLeft for both of them.
android:layout_marginLeft="10dp"

Both textView move to right 10dp.
Add paddingLeft for both of them.
android:paddingLeft="10dp"

Text move to right 10dp from edge.
So Margin affect to whole view, Padding  just affect text inside view.
If textView is narrow, we want some characters can encroach on right edge, we use padding.
<TextView
            android:layout_width="87dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/line2"
            android:text="Example text"
            android:singleLine="true"
            android:ellipsize="none"
            android:paddingRight="-5dp"
            android:layout_marginLeft="10dp"
            android:textColor="#800000"
            android:textSize="16sp" />


No comments:

Post a Comment