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

来源:互联网 发布:能写出这样的句子 知乎 编辑:程序博客网 时间:2024/04/29 18:15

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会影响这个控件在父控件 里的状态。


0 0
原创粉丝点击