1 LinearLayout
线性布局的一些属性:
xmlns:android:xml命名空间
android:orientation:布局方向
android:layout_width:
android:layout_height: 宽度和高度
控件的一些属性(
android中的界面显示单位主要有px,dp(dip),sp等
除了sp和dp,不要使用别的单位,除非你没有办法不那么做。
使用sp/dp会让你的Android应用适应多种密度和分辨率。
)
android:id
android:text
android:gravity:控件中内容的摆放位置
android:textSize:文本大小pt或px
android:background="#aa0000"
android:layout_width
android:layout_height
android:padding:内边框,分为上下左右四个方向
android:layout_weight:控件占父元素的比率
android:singleLine:控制是否换行显示
android:orientation:布局方向
android:layout_width:
android:layout_height: 宽度和高度
控件的一些属性(
android中的界面显示单位主要有px,dp(dip),sp等
除了sp和dp,不要使用别的单位,除非你没有办法不那么做。
使用sp/dp会让你的Android应用适应多种密度和分辨率。
)
android:id
android:text
android:gravity:控件中内容的摆放位置
android:textSize:文本大小pt或px
android:background="#aa0000"
android:layout_width
android:layout_height
android:padding:内边框,分为上下左右四个方向
android:layout_weight:控件占父元素的比率
android:singleLine:控制是否换行显示
2 TableLayout
每个子控件会占用一列
相关属性:
android:stretchColumns="1" 如果不能填满,则拉伸第一列
<TableLayout 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:stretchColumns="1"
>
<TableRow>
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
3 Layout的嵌套使用
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#aa0000"
android:text="red"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#00aa00"
android:text="green"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#0000aa"
android:text="blue"
android:layout_weight="1" />
</LinearLayout>
<TableLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:stretchColumns="0"
>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="row1_column1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="row1_column2" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="row2_column1"
android:gravity="center_horizontal" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="row2_column2"
android:gravity="center_horizontal" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="row2_column3"
android:gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
4 RelativeLayout
控件常用属性:与兄弟控件之间的位置关系:上下左右
android:layout_above="@id/label"
android:layout_below
android:layout_toLeftOf
android:layout_toRightOf
与兄弟控件的对齐方式:
android:layout_alignBaseline
android:layout_alignBottom="@id/ok"
android:layout_alignTop
android:layout_alignRight
android:layout_alignLeft
与 父控件的对齐方式:
android:layout_alignParentBottom="true"
android:layout_alignParentTop
android:layout_alignParentRight
android:layout_alignParentLeft
在父控件中的居中方式:
android:layout_centerHorizontal="true"
android:layout_centerInParent
android:layout_centerVertical
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"
/>
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="ok"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="cancel"
/>
</RelativeLayout>
5 AbsoluteLayout
指定子控件的xy精确坐标的布局。绝对布局缺乏灵活性,再没有绝对定位的情况下相比其他类型的布局更难维护。
6 FrameLayout
所有添加到这个布局中的视图都以层叠的方式显示。
第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
主要设置:android:layout_gravity="center"
layout.xml
<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#000011"
android:layout_gravity="center"
/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#004433"
android:layout_gravity="center"
/>
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#00aa00"
android:layout_gravity="center"
/>
<TextView
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#00dd00"
android:layout_gravity="center"
/>
</FrameLayout>