线性布局:有两种方式指定控件位置,一种是水平线性布局horizontal,一种是垂直线性布局vertical。
1、 android:gravity 与 android:layout_gravity的区别:
android:gravity是指定本元素的子元素相对它的对齐方式。
android:layout_gravity是指定本元素相对它的父元素的对齐方式
2、android:layout_weight通过设置控件的layout_weight属性控制各个控件在布局中的相对大。
3、android:layout_marginTop="10dp"使该控件与容器或下一个控件之间间隔10dp。
例子:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical">
-
<TextView android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:layout_marginTop="10dp" //使控件与容器相隔10dp
-
android:background="#000"/>
-
<TextView android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:layout_marginTop="10dp" //使这个控件与上一个控件相隔10dp
-
android:background="#808080"/>
-
<TextView android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:layout_marginTop="10dp"
-
android:background="#0000cd"/>
-
<TextView android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:layout_marginTop="10dp"
-
android:background="#32cd32"/>
-
<TextView android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:layout_weight="1"
-
android:layout_marginTop="10dp"
-
android:background="#ff0000"/>
-
</LinearLayout>
-
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="To" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Message"
android:gravity="top"/>
<Button android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="right"
android:hint="send"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"