Android应用开发学习之选项卡
在应用中使用选项卡,可以使复杂的布局变得简洁清晰。本文我们通过一个例子来学习Android选项卡的用法,下图是该例子的运行效果:
我们先来看主布局文件main.xml:
[html] view plaincopyprint?<?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">
<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">
</FrameLayout>
</LinearLayout>
</TabHost>
<?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">
<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">
</FrameLayout>
</LinearLayout>
</TabHost>
选项卡主要由TabHost、TabWidget、FrameLayout3个组件组成,三者缺一不可,想象一下选项卡的特点,多个卡重叠在一起,所以用FrameLayout即帧布局是必要的。另外需要注意的是TabHost、TabWidget、FrameLayout三个组件的android:id必须使用系统默认的名称,而不能自己随意定义,否则会出错。
接下来我们要看主Activity文件:
[java] view plaincopyprint?package com.liuhaoyu;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends Activity {
private TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost=(TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
inflater.inflate(R.layout.tab3, tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("FriendTab")
.setIndicator("Friend")
.setContent(R.id.LinearLayout01));
tabHost.addTab(tabHost.newTabSpec("StrangerTab")
.setIndicator("Stranger")
.setContent(R.id.LinearLayout02));
tabHost.addTab(tabHost.newTabSpec("BlacklistTab")
.setIndicator("Blacklist")
.setContent(R.id.LinearLayout03));
}
}
package com.liuhaoyu;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends Activity {
private TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t
补充:移动开发 , Android ,