Android中padding与layout_margin的区别与用法

本文详细解释了Android开发中layout_margin和padding的区别与用法。layout_margin用于定义View与其他View间的距离,而padding则用于定义View内容与View边界间的距离。通过具体的XML示例展示了两种属性的不同效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、定义   

        android:layout_margin就是设置view的上下左右边框的额外空间

        android:padding是设置内容相对view的边框的距离

        padding,含义为“填充”,像垫肩压类似的填充物,一个控件的padding及此控件内部的填充,由此可见padding是以所被定义的控件A为parent控件,而内部的内容物与控件A的间距。而layout_margin是A控件所在的控件为parent控件,是A与其的间距。

        其实概念很简单,padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了

 

当按钮分别设置以上两个属性时,得到的效果是不一样的。
android:paddingLeft="30px"
按钮上设置的内容(例如图片)离按钮左边边界30个像素
android:layout_marginLeft="30px"
整个按钮离左边设置的内容30个像素

二、使用示例

1、两个都不加

<?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"
    android:background="#ffffff" >
 
    <TextView
        android:layout_width="100dip"
        android:layout_height="100dip"
         android:background="#000000"
        android:text="Hello" />
 
</RelativeLayout>

 

2、只加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"
     android:padding ="100dip"
    android:background="#ffffff" >
 
    <TextView
        android:layout_width="100dip"
        android:layout_height="100dip"
         android:background="#000000"
        android:text="Hello" />
 
</RelativeLayout>

效果:

3、只加margin

<?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"
    android:background="#ffffff" >
 
    <TextView
        android:layout_width="100dip"
        android:layout_height="100dip"
         android:background="#000000"
         android:layout_margin="30dip"
        android:text="Hello" />
 
</RelativeLayout>
效果:

三、注意说明

        在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的

        在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的

        在AbsoluteLayout中,没有android:layout_margin属性

        在此提醒,xml参数中含有layout的参数项目为定义的控件相对于parent的关联,没有的一般为本身的定义,以上内容与此相符。又类似于gravity跟layout_gravity,带layout的是相对于parent的大体位置,而不带的是自身内部内容的大体位置。

 

转自:Android中padding与layout_margin的区别与用法

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="model" type="com.fshr.app.bean.sms.MyJobApplyBean" /> <import type="com.fshr.baseproject.utils.ConfigData" /> </data> <androidx.cardview.widget.CardView android:id="@+id/parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardCornerRadius="12dp" app:cardElevation="2dp"> <!-- Reduced elevation for a flatter look --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- Increased padding --> <!-- Application ID --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="申请表ID:" android:textColor="@color/grey_600" <!-- Lighter gray --> android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.id}" android:textColor="@color/black" android:textSize="16sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:background="@drawable/rounded_corner_reason_bg" android:gravity="center" android:paddingHorizontal="8dp" android:paddingVertical="4dp" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_work_appliacnt_status,model.applicationStatus,`待提交`)}" app:txColor="@{ConfigData.INSTANCE.getParamColor(ConfigData.INSTANCE.busi_work_appliacnt_status,model.applicationStatus)}" tools:text="Type" /> </LinearLayout> <!-- Applicant Information --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" <!-- Increased spacing --> android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关联作业项目:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.project.projectName}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <!-- Receiver Information --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="申请编号:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.project.operationNo}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <!-- Expected Access Time --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否提级审批:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isHighLevel)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否有危险源辨识和风险评价表:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isHazard)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否有作业方案及评审情况:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isWorkPlan)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="其他安全防护措施:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginTop="8dp" android:background="@drawable/rounded_corner_reason_bg" android:text="@{model.otherSafe }" android:textColor="@color/black" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_risk" android:layout_marginStart="8dp" android:text="管理危险辨识风险评价" android:textColor="@color/blue" android:textSize="16sp" /> <TextView android:id="@+id/text_scheme" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:gravity="right" android:visibility="@{model.isWorkPlan().equals(`1`)}" android:text="管理作业方案" android:textColor="@color/blue" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/lin_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:orientation="horizontal" android:visibility="@{!ConfigData.INSTANCE.jobJsUpdate(model.applicationStatus)}"> <Button android:id="@+id/btn_appover" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/button_approve_background" android:stateListAnimator="@null" android:text="修改" android:textColor="@color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_reject" android:layout_width="0dp" android:layout_height="40dp" android:layout_marginStart="16dp" android:layout_weight="1" android:background="@drawable/button_reject_background" android:stateListAnimator="@null" android:text="删除" android:textColor="@color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </layout> 从美观上帮我重新设计下排版,要求输入完整内容
最新发布
07-11
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context="com.jd.projects.wlw.shuhai.ShuhaiSp"> <!-- <!– 顶部工具栏 –>--> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#44bd32"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="90dp" android:layout_above="@+id/thumbnail_scrollview" android:layout_below="@id/toolbar" android:layout_marginTop="0dp" android:background="@android:color/black"> <!-- <ProgressBar--> <!-- android:id="@+id/video_loading"--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_gravity="center" />--> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="400dp" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="match_parent" android:layout_height="290dp" android:layout_alignParentBottom="true" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:alignmentMode="alignBounds" android:columnCount="2" android:padding="8dp" android:rowCount="0" android:background="#ffffff" android:useDefaultMargins="true"> <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> </android.widget.GridLayout> </LinearLayout> </ScrollView> </RelativeLayout>解决布局文件中<ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" />缩略图没有显示在<android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:alignmentMode="alignBounds" android:columnCount="2" android:padding="8dp" android:rowCount="0" android:background="#ffffff" android:useDefaultMargins="true">这一模块中
06-27
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值