Android 开发中Layout_Margin与padding的区别以及Layout_gravity与gravity的区别

本文详细解析了Android开发中常用的布局属性,如Layout_Margin与padding的区别,以及Layout_gravity与gravity的不同之处。通过实例展示了这些属性如何影响视图的外观及布局。

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

Layout_Margin与padding的区别以及Layout_gravity与gravity的区别

平时开发中这几个属性是我们经常使用的几个属性,偶尔脑子一糊涂,就容易弄混这些属性,下面,我就仔细介绍一下这几个属性:


1.首先介绍Layout_Margin与padding:

1.1.不设置任何Layout_Margin或者padding属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/groupView"
    android:background="#22aaa4"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:background="#9944aa"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        android:id="@+id/btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe2"/>
</LinearLay

上面xml中没有设置任何Layout_Margin或者padding属性,显示效果如下:


1.2设置padding属性:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/groupView"
    android:background="#22aaa4"
    <span style="color:#ff0000;">android:padding="50dp"</span>
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:background="#9944aa"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        android:id="@+id/btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe2"/>
</LinearLayout>

上面再LinearLayout里设置了padding属性,显示效果如下:



可以看见LinearLayout内部的控件全部都离边框一段距离了

下面继续:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/groupView"
    android:background="#22aaa4"
   <span style="color:#ff0000;"> android:padding="50dp"</span>
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:background="#9944aa"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        android:id="@+id/btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
       <span style="color:#ff0000;"> android:paddingTop="40dp"</span>
        android:text="hehe2"/>
</LinearLayout>

在之前的基础上,又在第二个Button中添加了一个padding属性,显示效果如下:



清晰了没有?原来padding就是会影响你设置了padding属性的这个控件的内部的状态,。

1.3.设置Layout_Margin:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/groupView"
    android:background="#22aaa4"

    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:background="#9944aa"
        android:layout_width="match_parent"
        android:layout_height="100dp"
       <span style="color:#ff0000;"> android:layout_margin="100dp"</span>
        android:text="hehe"
        android:id="@+id/btn"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe2"/>
</LinearLayout>
上面就在第一个Button中设置了margin值,看显示效果与第一张没设置任何东西得图作对比哦:




在设置一个看看:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/groupView"
    android:background="#22aaa4"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:background="#9944aa"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        <span style="color:#ff0000;">android:layout_margin="100dp"</span>
        android:text="hehe"
        android:id="@+id/btn"/>
    <Button
        android:layout_width="match_parent"
       <span style="color:#ff0000;"> android:layout_marginTop="100dp"</span>
        android:layout_height="100dp"
        android:text="hehe2"/>
</LinearLayout>

显示效果如下:



看出来没有?layout_Margin就是指你设置了这个属性的控件,在他的父控件里与其他的控件之间的位置,千万不要以为这个是就是子控件在父控件里的相对位置哦!这样表达式不正确的啊,第二幅图就直接说明了这一点,是控件之间的关系,


总结:padding是指设置了这个属性的控件内部的位置变化关系,比如TextView中或者Button中设置了的话,那么这些控件上面显示的text位置就会发生相应的变化,如果在父控件中设置了这个属性,比如LinearLayout中设置了这个属性,那么它内部的子控件的位置就会发生变化!
          Layout_Margin是指设置了这个属性的控件,与他平级的控件之间的位置关系,(Ps:padding是内部的位置关系)!


2.Gravity与Layout_gravity之间的关系:

2.1不设置任何东西:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#44aa77"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        android:textSize="30sp"
        android:background="#aa2288"/>
</LinearLayout>



设置gravity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#44aa77"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        <span style="color:#ff0000;">android:gravity="center"</span>
        android:textSize="30sp"
        android:background="#aa2288"/>
</LinearLayout>


gravity属性,会影响设置了这个属性的控件的内部的状态


2.只设置Layout_gravity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="#44aa77"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="hehe"
        <span style="color:#ff0000;">android:layout_gravity="center"</span>
        android:textSize="30sp"
        android:background="#aa2288"/>
</LinearLayout>



设置了layout_gravity这个属性,会影响设置了这个属性的控件相对他的父控件里的位置变化。


结论:gravity影响控件内部状态,Layout_gravity会影响这个控件在父控件 里的状态。


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"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> <GridLayout android:id="@+id/thumbnail_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/footer_buttons" android:layout_below="@id/video_container" android:layout_marginBottom="300dp" android:background="@android:color/white" android:columnCount="4" android:orientation="horizontal" android:padding="8dp" android:rowCount="2"> <!-- 动态添加的缩略图将放在这里 --> <!-- 示例缩略图 (可选) --> <ImageView android:layout_width="50dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" /> <ImageView android:layout_width="50dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" /> <!-- 更多缩略图... --> </GridLayout> </RelativeLayout> </RelativeLayout> 把布局文件中android:id="@+id/thumbnail_grid"放置在播放控制条的下面
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值