1、padding,
android:paddingLeft="20dp"
android:paddingRight="20dp"
对下层布局或控件的约束。
2、margin
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
对上层布局的约束。
例:
<?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:paddingLeft="20dp"
android:paddingRight="20dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text="Hello World!" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
效果:

代码:
本文详细解析了Android开发中Padding与Margin的区别与应用,通过实例代码展示了这两个属性如何影响布局元素的内外间距,是Android开发者理解界面设计布局的重要参考资料。
690

被折叠的 条评论
为什么被折叠?



