菜鸟学习android之TabHost.查询资料说android有这么个玩意--------选项卡。首先聊聊什么是选项卡,从网上搜索资料显示:一个顶部的按钮(可点击的)的切换卡部分,一个主内容区(上图显示“第二个窗体”字体的)的主显示区。举个简单点的例子吧,大家可以看看android手机上的通讯录,用的就是选项卡,点击顶部的选项卡一,二,三,在显示区会有不同的内容。强烈推荐看看手机上的通讯录。
TabHost是Tab的容器,包括两部分TabWidget和FrameLayout,TabWidget是tab的标签,FrameLayout是tab的内容。
再来说说,选项卡的使用:先来说说,xml布局文件的使用
1-----TabHost必须设置为@android:id/tabHost
2------TabWidget必须将android:id设置为@android:id/tabs
3-------FrameLayout需要将android:id设置为@android:id/tabcontent
接下来说说Activity的使用。如果想新建一个Activity实现tabhost,必须要继承TabActivity,此后,又是三步走战略。
1-------获得TabHost的对象,
TabHost tabHost = getTabHost();
2-------通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator()增加页的标签。
TabHost.TabSpec spec = tabHost.newTabSpec();
spec.setContent(new Intent());
spec.setIndicator("音乐",Resource res);
tabHost.addTab(spec);
3--------通过setCurrentTab(index)指定显示的页,从0开始。
tabHost.setCurrentTab(0);
以下是代码。
- package cn.com.karl.music;
-
- import android.app.TabActivity;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.TabHost;
-
- /**
- * 选项卡的使用 1 . 继承TabActivity 2、用getTabHost()方法获取TabHost; 3、各Tab内容在布局文件中定义。
- * 在手机屏幕中,Tab也是比较常用的,通常和List结合,例如我们手机的通信录。下面是Tag的结构。
- *
- * TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,
- * FrameLayout则是tab内容。
- *
- * 如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
- * TabWidget必须设置android:id为@android:id/tabs
- * FrameLayout需要设置android:id为@android:id/tabcontent
- *
- * @author SYJ
- *
- */
- public class MainActivity extends TabActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);// 应用窗体显示状态操作该参数表示无标题
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉任务栏
- setContentView(R.layout.main);// 该语句必须在以上两句之后
-
- Resources res = getResources(); // 得到资源对象
- //步骤1:获得TabHost的对象,并进行初始化setup()
- TabHost tabHost = getTabHost(); // 选项卡类似于java中的卡片布局
- TabHost.TabSpec spec;
- Intent intent;
- intent = new Intent().setClass(this, ListActivity.class);// 启动音乐列表界面
- // 步骤2:通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签
- spec = tabHost.newTabSpec("音乐")
- .setIndicator("音乐", res.getDrawable(R.drawable.item))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, ArtistsActivity.class);
- spec = tabHost.newTabSpec("艺术家")
- .setIndicator("艺术家", res.getDrawable(R.drawable.artist))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, AlbumsActivity.class);
- spec = tabHost.newTabSpec("专辑")
- .setIndicator("专辑", res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, SongsActivity.class);
- spec = tabHost.newTabSpec("最近播放")
- .setIndicator("最近播放", res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
-
- // 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算。
- tabHost.setCurrentTab(0);
-
- }
- }
- <?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:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- 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>
- package cn.com.karl.music;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.TabHost;
- /**
- * 选项卡的使用 1 . 继承TabActivity 2、用getTabHost()方法获取TabHost; 3、各Tab内容在布局文件中定义。
- * 在手机屏幕中,Tab也是比较常用的,通常和List结合,例如我们手机的通信录。下面是Tag的结构。
- *
- * TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,
- * FrameLayout则是tab内容。
- *
- * 如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
- * TabWidget必须设置android:id为@android:id/tabs
- * FrameLayout需要设置android:id为@android:id/tabcontent
- *
- * @author SYJ
- *
- */
- public class MainActivity extends TabActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);// 应用窗体显示状态操作该参数表示无标题
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉任务栏
- setContentView(R.layout.main);// 该语句必须在以上两句之后
- Resources res = getResources(); // 得到资源对象
- //步骤1:获得TabHost的对象,并进行初始化setup()
- TabHost tabHost = getTabHost(); // 选项卡类似于java中的卡片布局
- TabHost.TabSpec spec;
- Intent intent;
- intent = new Intent().setClass(this, ListActivity.class);// 启动音乐列表界面
- // 步骤2:通过TabHost.TabSpec增加tab的一页,通过setContent()增加内容,通过setIndicator增加页的标签
- spec = tabHost.newTabSpec("音乐")
- .setIndicator("音乐", res.getDrawable(R.drawable.item))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, ArtistsActivity.class);
- spec = tabHost.newTabSpec("艺术家")
- .setIndicator("艺术家", res.getDrawable(R.drawable.artist))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, AlbumsActivity.class);
- spec = tabHost.newTabSpec("专辑")
- .setIndicator("专辑", res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, SongsActivity.class);
- spec = tabHost.newTabSpec("最近播放")
- .setIndicator("最近播放", res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
- // 步骤3:可通过setCurrentTab(index)指定显示的页,从0开始计算。
- tabHost.setCurrentTab(0);
- }
- }
- <?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:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- 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>