原址:http://hualang.iteye.com/category/143855
LinearLayout(线性布局)
LinearLayout是在线性方向显示View元素的一个ViewGroup,可以是水平方向,也可以是垂直方向
你可以重复使用LinearLayout,如果你想使用嵌套多层的LinearLayout的话,你可以考虑使用RelativeLayout来替换.
1、开始创建一个工程名字叫做HelloLinearLayout
2、打开res/layout/main.xml文件并且插入如下内容
- <?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:text="red"
- android:gravity="center_horizontal"
- android:background="#aa0000"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <TextView
- android:text="green"
- android:gravity="center_horizontal"
- android:background="#00aa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <TextView
- android:text="blue"
- android:gravity="center_horizontal"
- android:background="#0000aa"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <TextView
- android:text="yellow"
- android:gravity="center_horizontal"
- android:background="#aaaa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="row one"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <TextView
- android:text="row two"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <TextView
- android:text="row three"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <TextView
- android:text="row four"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- </LinearLayout>
- </LinearLayout>
3、现在打开HelloLinearLayout.java并且确定它已经在onCreate()方法中加载了res/layout/main.xml布局文件
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
setContentView(int)方法为Activity加载了布局文件,由资源resource ID所指定---R.layout.main指的是res/layout/main.xml布局文件
4、运行程序,你可以看到如下的情况
RelativeLayout(相对布局)
RelativeLayout是一个在相对位置上显示子View元素的VeiwGroup,一个视图的位置,可以指定为相对于兄妹的元素(比如一个给定的与孙的左边或者下边)或者心爱那个对于RelativeLayout区域的位置(比如与底部对齐,剩下的中心)
一个RelativeLayout是一个非常强大使用的为设置用户界面的布局,因为它可以消除嵌套的视图组ViewGroup,如过你发现你用了几个嵌套的LinearLayout组,你可以替换为一个单独的RelativeLayout
1、开始一个新的工程,名字叫做HelloRelativeLayout
2、打开res/layout/main.xml文件并且插入如下信息
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <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="10dip"
- 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>
所有的layout布局属性都被定义在RelativeLayout.LayoutParams中
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
TableLayout(表格布局)
TableLayout是一个以行、列显示视图View的视图组
1、开始一个新的工程,名字叫做HelloTableLayout
2、打开res/layout/main.xml文件并且插入如下内容
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:stretchColumns="1">
- <TableRow>
- <TextView
- android:layout_column="1"
- android:text="Open..."
- android:padding="3dip"
- />
- <TextView
- android:text="Ctrl-O"
- android:gravity="right"
- android:padding="3dip"
- />
- </TableRow>
- <TableRow>
- <TextView
- android:layout_column="1"
- android:text="Save..."
- android:padding="3dip"
- />
- <TextView
- android:text="Ctrl-S"
- android:gravity="right"
- android:padding="3dip"
- />
- </TableRow>
- <TableRow>
- <TextView
- android:layout_column="1"
- android:text="Save as..."
- android:padding="3dip"
- />
- <TextView
- android:text="Ctrl-Shift-S"
- android:gravity="right"
- android:padding="3dip"
- />
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#FF909090"
- />
- <TableRow>
- <TextView
- android:text="X"
- android:padding="3dip"
- />
- <TextView
- android:text="Import..."
- android:padding="3dip"
- />
- </TableRow>
- <TableRow>
- <TextView
- android:text="X"
- android:padding="3dip"
- />
- <TextView
- android:text="Export..."
- android:padding="3dip"
- />
- <TextView
- android:text="Ctrl-E"
- android:gravity="right"
- android:padding="3dip"
- />
- </TableRow>
- <View
- android:layout_height="2dip"
- android:background="#FF909090"
- />
- <TableRow>
- <TextView
- android:layout_column="1"
- android:text="Quit"
- android:padding="3dip"
- />
- </TableRow>
- </TableLayout>
注意到这个文件类似于HTML的table的结构,TableLayout元素就像是HTML中的<table>元素;TableRow就像是一一个<tr>元素;但是对于每一个单元格,你可以用各种视图元素,在这里例子里,每个单元格用TextView,在这些行之间,还有一个基本View,用来画水平线
TextView中的一些属性
android:layout_column="1":表示控件放在标号为1的列上,标号是从0开始的
android:gravity="right":定义字体在父控件中显示在右边
android:stretchColumns="1":设置自动拉伸哪些列,列ID从0开始,多个列的话用","分隔。这里的作用是让第2列可以扩展到所有可用空间
android:shrinkColumns:设置自动收缩哪些列,列ID从0开始,多个列的话用","分隔
android:collapseColumns:设置自动隐藏哪些列,列ID从0开始,多个列的话用","分隔
顺便:android:layout_span表示一个控件占几列空间
下面的是基本的View,是在屏幕上画一条2dip高的一条横线
<View
android:layout_height="2dip"
android:background="#FF909090"
/>
3、运行结果如下:
Tab Layout(选项卡布局)
为了创建一个选项卡的UI,你需要使用一个TabHost和一个TabWidget,TabHost必须是布局文件的根节点,它包含了为了显示选项卡的TabWidget和一个用于显示选项内容的FrameLayout
你可以用一或两种方法实现你的选项卡内容:在用一个Activity中用选项卡来在视图之间切换,或者用用选项卡来改变所有的分离的Activity。你根据你的需求来使用你想在程序中的方法,但是如果每个选项卡提供一个独特的用户Activity,那么为每个选项卡实现独立的Activity是有意义的,所有你最好在你的离散群里管理应用程序,要好过使用大量的应用程序和布局文件。
在这个例子中,你可以创建一个为每个单独的Activity创建选项卡来创建一个选项卡UI
1、开始一个新的工程,叫做HelloTabWidget
2、第一,创建三个独立的Activity程序在你的工程中:ArtistsActivity,AlbumsActivity,和SongsActivity,他们每个代表一个单独的选项卡,现在用TextView来没每个程序显示一个简单的信息,比如:
- package org.hualang.tabwidget;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class AlbumsActivity extends Activity {
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- TextView textview = new TextView(this);
- textview.setText("This is the Albums tab");
- setContentView(textview);
- }
- }
- package org.hualang.tabwidget;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class SongsActivity extends Activity {
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- TextView textview = new TextView(this);
- textview.setText("This is the Songs tab");
- setContentView(textview);
- }
- }
- package org.hualang.tabwidget;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class ArtistsActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- TextView textview = new TextView(this);
- textview.setText("This is the Artists tab");
- setContentView(textview);
- }
- }
注意这个例子中不需要布局文件,只需要创建一个TextView,并且为文本赋值即可。重复创建三个类似的Activity,并且要在AndroidManifest.xml文件中注册,否则报错
3、你需要为每个选项卡设置一个icon,每个icon,你可以有两个版本,一个是当选项卡被选中的时候,另一个是当选项卡未被选中的时候。一般设计来说,建议当被选中的时候用灰色,的那个未被选中的时候用白色,比如
你可以拷贝这两张图片来做实验用
现在创建一个状态图片列表来制定每个选项卡不同状态的时候所指定的图片
①把图排尿保存在res/drawable/目录下
②在res/drawable/目录下创建一个名为ic_tab_artists.xml文件,并且插入如下信息
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- When selected,use grey -->
- <item android:drawable="@drawable/ic_tab_artists_grey"
- android:state_selected="true"/>
- <!-- When not selected ,use white -->
- <item android:drawable="@drawable/ic_tab_artists_white"/>
- </selector>
上面这个文件,当选项卡的状态改变的时候,选项卡就会自动的在两种已经定义的图片之间切换
4、打开res/layout/main.xml文件并且插入如下信息
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="5dp">
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="5dp" />
- </LinearLayout>
- </TabHost>
这个布局文件将显示选项卡兵器提供每个Activity之间的导航
TabHost要求一个TabWidget和一个FrameLayout布局,为了使TabWidget和FrameLayout的位置处于垂直方向,需要一个LinearLayout,FrameLayout是每个选项卡内容的地方,之所以那里的内容是空的是因为在TahHost中将自动为每个Activity嵌入
注意,TabWidget和FrameLayout元素的ID标签和tabcontent元素,这些名称必须使用,因为TahHost检索他们的引用,它恰好期望的是这些名字
6、编写HelloTabWidget。继承TabWidget
- package org.hualang.tabwidget;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.widget.TabHost;
- public class HelloTabWidget extends TabActivity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Resources res = getResources(); // Resource object to get Drawables
- TabHost tabHost = getTabHost(); // The activity TabHost
- TabHost.TabSpec spec; // Resusable TabSpec for each tab
- Intent intent; // Reusable Intent for each tab
- // Create an Intent to launch an Activity for the tab (to be reused)
- intent = new Intent().setClass(this, ArtistsActivity.class);
- // Initialize a TabSpec for each tab and add it to the TabHost
- spec = tabHost.newTabSpec("artists").setIndicator("Artists",
- res.getDrawable(R.drawable.ic_tab_drawable))
- .setContent(intent);
- tabHost.addTab(spec);
- // Do the same for the other tabs
- intent = new Intent().setClass(this, AlbumsActivity.class);
- spec = tabHost.newTabSpec("albums").setIndicator("Albums",
- res.getDrawable(R.drawable.ic_tab_drawable))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, SongsActivity.class);
- spec = tabHost.newTabSpec("songs").setIndicator("Songs",
- res.getDrawable(R.drawable.ic_tab_drawable))
- .setContent(intent);
- tabHost.addTab(spec);
- tabHost.setCurrentTab(2);
- }
- }
运行结果: