Android | XML相對佈局 | margin與padding的區別

概述

先來看一張圖片:

在這裏插入圖片描述
可以看到margin叫做偏移,就是上下左右幾個方向,各自偏移多少

padding叫做填充,指的是在組件內的上下左右四個方向,各自填充多少

效果圖

在這裏插入圖片描述

代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Button 
       	android:id="@+id/btn1"
        android:text="btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

	<Button
	    android:id="@+id/btn2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
		android:text="btn2"
	    android:layout_toRightOf="@id/btn1"
	    android:paddingLeft="100dp" />
	<Button 
	    android:id="@+id/btn3"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="btn3"
	    android:layout_alignParentBottom="true"
	    />
	<Button 
	    android:id="@+id/btn4"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="btn4"
	    android:layout_alignParentBottom="true"
	    android:layout_toRightOf="@id/btn3"
	    android:layout_marginLeft="100dp"
	    />
</RelativeLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章