您的位置:首页 > 移动开发 > Android开发

Android 选项卡的使用

2013-12-02 17:44 525 查看
废话不说,代码如下:

首先创建一个xml(TabHost)文件。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

tab1与选项卡tab1对应
<LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent">			<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab1"/></LinearLayout>

tab2与选项卡tab2对应
<LinearLayoutandroid:id="@+id/tab2" android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="我是tab2"/></LinearLayout></TabHost>


import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LayoutInflater layoutInflater = LayoutInflater.from(this);

Resources res = getResources();
TabHost host = getTabHost();
TabSpec spec;
layoutInflater.inflate(R.layout.tab,host.getTabContentView(),true );

spec=host.newTabSpec("1")
.setIndicator("tab1",     //选项卡的名称
res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的
.setContent(R.id.tab1);//设置点击选项卡所显示的内容

host.addTab(spec);      //添加选项卡

spec=host.newTabSpec("2")
.setIndicator("tab2",      //选项卡的名称

res.getDrawable(android.R.drawable.star_big_on))//选项卡的ICON,androdi.R 是android自带的

.setContent(R.id.tab2);           //设置点击选项卡所显示的内容

host.addTab(spec);     //添加选项卡

host.setCurrentTab(0);  //用来设置哪个选项卡是默认选项卡  从0开始

}
}


效果如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: