Android:padding和android:layout_margin的区别

本文详细解释了Android布局中Padding和Margin的区别。Padding定义了View内部内容与其边界之间的距离,而Margin则指定了View与其他View之间的距离。文章还讨论了不同布局类型下这两个属性的有效性。

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

Android:padding和android:layout_margin的区别:
  padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了;
 android:padding是设置内容相对view的边框的距离,Android:layout_margin就是设置view的上下左右边框的额外空间;
 在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的
 在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的
 在AbsoluteLayout中,没有android:layout_margin属性.
<!-- 在原有布局基础上添加校准面板 --> <LinearLayout android:id="@+id/calibrationPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#80000000" android:padding="16dp" android:visibility="gone" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="颜色校准" android:textSize="20sp" android:textColor="@android:color/white" android:textStyle="bold"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="8dp"> <Button android:id="@+id/btnCaptureWhite" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="白色校准" android:backgroundTint="#9E9E9E"/> <Button android:id="@+id/btnCaptureBlack" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="黑色校准" android:backgroundTint="#212121" android:layout_marginStart="8dp"/> </LinearLayout> <Button android:id="@+id/btnCaptureCustom" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义校准" android:layout_marginTop="8dp"/> <Button android:id="@+id/btnResetCalibration" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="重置校准" android:backgroundTint="#F44336" android:textColor="@android:color/white" android:layout_marginTop="8dp"/> <TextView android:id="@+id/tvCalibrationStatus" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="校准状态: 未校准" android:textSize="14sp" android:textColor="#CCCCCC" android:layout_marginTop="8dp"/> </LinearLayout> 此布局是之前回复的答案,但是没看到btnCalibrationToggle这个控件id,,
最新发布
07-31
<?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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值