Android基本布局
<1>Android中的布局
LinearLayout(线性布局)
特点:元素会一个接一个的排列。
方向:orientation 水平的:vertical 水平的:horizontal
再有就是–> wrap_content:匹配内容的大小
match_content:匹配父容器的大小,其中fill_match和他的作用是一样的。只是fill_match出现的较早。
layout_weight:权重,控制空间所占的比例,通常使用形式为layout_weight+0dp结合。
来一个小Demo:
<LinearLayou 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:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/num" />
</LinearLayout>
注意:
- 线性布局可以相互嵌套
RelativeLayout(相对布局)
相对布局就是里面的元素的位置都是以相对位置来确定的,可以是相对父容器,也可以是相对其他元素。
几个属性:
- android:layout_toLeftOf、android:layout_toRightOf、android:layout_above、android:layout_below分别表示位于XXX的左方,右方,上方和下方,后面接元素的iD.
- android:layout_alignLeft、android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom分别表示和什么元素对其。
- android:layout_alignParentRight、android:layout_alignParentLeft、android:layout_alignParentTop、android:layout_alignParentBottom、android:layout_centerInParent、android:layout_centerHorizontal、android:layout_centerVertical分别表示相对于父容器的右,左、上、下、居中,水平居中和垂直居中。
来一个小Demo:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/centerButton"
android:text="左"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/centerButton"
android:text="右"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_above="@id/centerButton"
android:text="上"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/centerButton"
android:text="下"
/>
</RelativeLayout>
margin:外边距,即控件与控件之间的间距
padding:内边距,即控件中的内容和控件的距离
前面的两种布局要重点掌握,后面的三种布局了解就行。
FrameLayout(帧布局)
特点:一层一层的显示。
直接上Demo:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:gravity="center"
android:text="TextView" />
</FrameLayout>
注意:android:gravity=”center” 表示的是控件中的内容居中, android:layout_gravity=”center”表示控件居中
TableLayout(表格布局)
表格布局相当于线性布局的子布局,可以完全用线性布局来实现。
Demo:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button2"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button3"/>
</TableRow>
</TableLayout>
表示一行。android:layout_weight表示权重
AbsoluteLayout(绝对布局)
绝对布局就是利用横纵坐标指定元素的具体位置。
这种布局使得元素定位太过呆板,很少用。
Demo:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="103dp"
android:layout_y="52dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="115dp"
android:layout_y="108dp"
android:text="Button" />
</AbsoluteLayout>