padding和layout_margin的区别

顾名思义,padding有填充物的意思,而layout_margin中,layout是布局的意思,margin有边距、间隙的意思。所以,和layout_gravitygravity 的区别有所类似,

layout_gravity:容器或组件自身在整个布局中的排列方式;gravity:容器中的组件在容器中的排列方式或是组件中的文本、图片等元素在组件中的排列方式。带有layoutlayout_margin指的是调用该属性对象的本身与其他容器或组件的间隙,而padding指的是调用该属性的对象里的内容与该对象本身有填充物占据。

这里举一个布局嵌套布局的例子:

<?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" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_margin="20dp"

        android:orientation="vertical">

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="20dp"

            android:padding="20dp"

            android:text="@string/basketball" />

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="20dp"

            android:padding="20dp"

            android:text="@string/pingpong" />

 

        <Button

            android:id="@+id/btn3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="20dp"

            android:padding="20dp"

            android:text="@string/football" />

    </LinearLayout>

 

</LinearLayout>

在该例子中,内嵌linearLayout布局容器的属性里,layout_margin显示的是该容器与整体的linearLayout上、右、下、左的边距均为20dp

同样道理,在三个button组件的属性里,layout_margin显示的是组件与外部容器的四个边距均为20dp,而padding显示的是三个组件内部的文本内容与组件四个边线的填充距离均为20dp

<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/round_style" android:elevation="4dp" android:layout_margin="8dp" android:padding="10dp"> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/spec" android:paddingTop="2dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/style" app:layout_constraintStart_toEndOf="@+id/spec" app:layout_constraintTop_toTopOf="parent"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/name" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/spec"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/real_inventory" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/name"/> <com.xuexiang.xui.widget.textview.autofit.AutoFitTextView style="@style/item_show_title" android:id="@+id/check_inventory" app:layout_constraintStart_toEndOf="@+id/real_inventory" app:layout_constraintTop_toBottomOf="@+id/name"/> <ImageView android:id="@+id/status" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/name" app:layout_constraintTop_toTopOf="parent" /> <View style="@style/item_show" android:id="@+id/view_task_list" android:layout_width="match_parent" android:layout_height="1dp" android:background="#cccccc" android:layout_marginTop="3dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@+id/real_inventory" app:layout_constraintStart_toStartOf="@+id/real_inventory"/> </androidx.constraintlayout.widget.ConstraintLayout>加入button功能,但并不影响源码
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值