android:padding与layout_margin gravity与layout_gravity

本文解析了Android中padding与margin的区别,以及gravity与layout_gravity的不同之处。padding定义了子视图与父视图边界间的距离,而margin定义了当前视图与其他视图的距离。gravity用于设置视图内部元素的位置,layout_gravity则定义了视图在其父布局中的位置。

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

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

2.在Android的LinearLayout布局中,有两个gravity属性,都是设置组件的位置,它们是android:gravity和android:layout_gravity,那么它们之间有什么区别,分别用在什么地方? 

由两者的名称我们可以看出,一个是基于Layout的,而另一个则不是。那么相对而言,android:gravity属性设置的是控件自身上面的内容位置,而android:layout_gravity属性则设置控件本身相对于父控件的显示位置。

以一个简单例子来看,假设布局中有一个按钮,那么android:gravity设置了按钮上面的文字显示位置,而android:layout_gravity设置了按钮在布局中的显示位置。 

<?xml version="1.0" encoding="utf-8"?> <layout xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="viewModel" type="com.tplink.tpmifi.viewmodel.SmsBoxViewModel" /> </data> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:addStatesFromChildren="true" android:background="@drawable/toolbar_background" android:minHeight="@dimen/titlebar_height"> <LinearLayout android:id="@+id/title_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginStart="@dimen/titlebar_left_margin_start" android:background="@drawable/btn_bg" android:clickable="true" android:focusable="true" android:gravity="center" android:minWidth="@dimen/titlebar_height" android:minHeight="@dimen/titlebar_height" android:orientation="horizontal" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/title_left_icon" android:layout_width="@dimen/titlebar_action_image_size" android:layout_height="@dimen/titlebar_action_image_size" android:scaleType="centerInside" android:src="@drawable/arrow_back_white" android:visibility="visible" /> </LinearLayout> <com.tplink.design.text.TPTextView android:id="@+id/title_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="@dimen/titlebar_title_name_margin_start" android:layout_toEndOf="@+id/title_left" android:layout_weight="1" android:fontFamily="sans-serif-medium" android:lines="1" android:textAlignment="gravity" android:textColor="@android:color/white" android:textSize="@dimen/tpds_all_text_size_18" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0" app:layout_constraintStart_toEndOf="@id/title_left" app:layout_constraintTop_toTopOf="parent" tools:text="Outbox" /> <com.tplink.design.text.TPTextView android:id="@+id/title_right_text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginEnd="20dp" android:layout_toStartOf="@+id/title_right_text2" android:background="@drawable/btn_bg" android:gravity="center" android:lines="1" android:padding="@dimen/tpds_all_dp_16" android:text="@{viewModel.isAllItemSelected ? @string/common_deselect_all :@string/common_select_all }" android:textAlignment="gravity" android:textColor="@color/white" android:textSize="@dimen/tpds_all_text_size_16" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/title_right_text2" app:layout_constraintHorizontal_bias="1" app:layout_constraintStart_toEndOf="@id/title_name" app:layout_constraintTop_toTopOf="parent" app:visibleOrGone="@{viewModel.editing}" tools:visibility="visible" /> <com.tplink.design.text.TPTextView android:id="@+id/title_right_text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:layout_marginEnd="16dp" android:background="@drawable/btn_bg" android:clickable="@{viewModel.totalMsgNum != 0 }" android:gravity="center" android:lines="1" android:padding="@dimen/tpds_all_dp_16" android:text="@{viewModel.editing ? @string/common_cancel : @string/common_edit }" android:textAlignment="gravity" android:textColor="@{viewModel.totalMsgNum != 0 ? @color/white : @color/title_bar_text_non_clickable }" android:textSize="@dimen/tpds_all_text_size_16" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1" app:layout_constraintStart_toEndOf="@id/title_right_text1" app:layout_constraintTop_toTopOf="parent" tools:text="@string/common_edit" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>
07-11
shu3.xml:<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context=".ShuHai3Activity"> <!-- Toolbar --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <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="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.55" android:background="@android:color/black" app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.6" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintVertical_bias="0.0"> <!-- 模拟萤石播放加载动画 --> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <!-- 视频播放View,可以替换成具体的视频播放控件 --> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.45" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 多个缩略图子项 --> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> 修改下面shuhaisp.xml布局上面shu3.xml布局一样,保持shuhaisp.xml依赖不变必须使用android支持库。shuhaisp.xml:<?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=".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="0dp" android:layout_below="@id/toolbar" android:layout_above="@+id/thumbnail_scrollview" 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="match_parent" 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="0dp" 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="wrap_content" android:columnCount="2" android:rowCount="0" android:alignmentMode="alignBounds" android:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <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>
06-25
我说了只要相对分布不要擅自修改:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:background="#F5F5F5"> <LinearLayout android:layout_width="396dp" android:layout_height="22dp" android:background="#E0E0E0" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:orientation="horizontal"> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:contentDescription="Signal" android:src="@drawable/ic_signal" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="4dp" android:contentDescription="Battery" android:src="@drawable/ic_battery" /> </LinearLayout> </LinearLayout> <ImageView android:layout_width="48dp" android:layout_height="48dp" android:src="@drawable/Deco_App" android:contentDescription="Deco Logo" android:layout_gravity="center" android:layout_marginVertical="16dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Deco" android:textSize="20sp" android:textStyle="bold" android:textColor="#666666" android:layout_gravity="center" android:layout_marginBottom="16dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I accept the Terms of Use and confirm that I have fully read and understood the Privacy Policy." android:textSize="12sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="8dp"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I confirm to join the User Experience Improvement Program. I understand that I can opt out of the program any time." android:textSize="14sp" android:textColor="#666666" android:buttonTint="#00C4CC" android:layout_marginBottom="16dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Continue" android:textColor="#FFFFFF" android:background="@drawable/rounded_button" android:layout_gravity="center" android:textSize="16sp" android:layout_marginBottom="8dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Disagree and Quit" android:layout_gravity="center" android:textSize="14sp" android:textColor="#00C4CC" /> </LinearLayout>
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值