Android之TabHost的几种使用方法
来源:互联网 发布:数据冗余类型 编辑:程序博客网 时间:2024/04/30 01:34
1,Activity要继承TabActivity,使用LayoutInflater方式来初始化布局文件。
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"><LinearLayout android:id="@+id/tab01"android:layout_width="fill_parent" android:layout_height="fill_parent"android:gravity="center_horizontal" android:orientation="vertical"><TextView android:id="@+id/tv01" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textSize="24dip"android:text="这是选项卡一,你课可以在这里添加你要显示的控件" /></LinearLayout><LinearLayout android:id="@+id/tab02"android:layout_width="fill_parent" android:layout_height="fill_parent"android:gravity="center_horizontal" android:orientation="vertical"><TextView android:id="@+id/tv02" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textSize="24dip"android:text="这是选项卡二,你课可以在这里添加你要显示的控件" /></LinearLayout><LinearLayout android:id="@+id/tab03"android:layout_width="fill_parent" android:layout_height="fill_parent"android:gravity="center_horizontal" android:orientation="vertical"><TextView android:id="@+id/tv03" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textSize="24dip"android:text="这是选项卡三,你课可以在这里添加你要显示的控件" /></LinearLayout></LinearLayout>
TabHostActivity.java
package com.test;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;public class TabHostActivity extends TabActivity { /** Called when the activity is first created. */TabHost myTabHost ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); myTabHost=this.getTabHost(); LayoutInflater.from(this).inflate(R.layout.main, myTabHost.getTabContentView(), true); myTabHost.addTab(myTabHost.newTabSpec("选项卡一").setIndicator("选项卡一", getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab01)); myTabHost.addTab(myTabHost.newTabSpec("选项卡二").setIndicator("选项卡二", getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02)); myTabHost.addTab(myTabHost.newTabSpec("选项卡三").setIndicator("选项卡三", getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab03)); }}
2,同样继承TabActivity,但是选项卡的内容则是跳转到不同的activity。
mian.xml
<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>
注意:tabhost中的id,tabwidget的id以及framelayout的id
TabHostActivity.java
package com.test;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;import android.widget.TabHost;public class TabHostActivity extends TabActivity { /** Called when the activity is first created. */TabHost myTabHost ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//得到TabHost对象,正对TabActivity的操作通常都有这个对象完成TabHost tabHost = getTabHost();//生成一个Intent对象,该对象指向一个ActivityIntent activity1Intent = new Intent();activity1Intent.setClass(this, Activity1.class);//生成一个TabSpec对象,这个对象代表了一个页TabHost.TabSpec activity1Spec = tabHost.newTabSpec("选项卡一");Resources res = getResources();//设置该页的indicator , 这里使用了系统自动的图片资源activity1Spec.setIndicator("选项卡一", res.getDrawable(android.R.drawable.stat_sys_download));//设置该页的内容activity1Spec.setContent(activity1Intent);//将设置好的TabSpec对象添加到TabHost当中tabHost.addTab(activity1Spec);Intent activity2Intent = new Intent();activity2Intent.setClass(this, Activity2.class);TabHost.TabSpec activity2Spec = tabHost.newTabSpec("选项卡二");activity2Spec.setIndicator("选项卡二", res.getDrawable(android.R.drawable.stat_sys_upload));activity2Spec.setContent(activity2Intent);tabHost.addTab(activity2Spec); }}
注意:选项卡中的两个activity的代码就不贴上了。
3,不继承TabActivity。
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/hometabs" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这里是选项卡一"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这里是选项卡二"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="这里是选项卡三"/> </FrameLayout> </LinearLayout> </TabHost></LinearLayout>
TabHostActivity.java
package com.test;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;public class TabHostActivity extends Activity { /** Called when the activity is first created. */TabHost myTabHost ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); tabHost.setup(); tabHost.addTab(tabHost .newTabSpec("选项二") .setIndicator("选项二", getResources().getDrawable(R.drawable.icon)) .setContent(R.id.view1)); tabHost.addTab(tabHost .newTabSpec("选项二") .setIndicator("选项二", getResources().getDrawable(R.drawable.icon)) .setContent(R.id.view2)); tabHost.addTab(tabHost .newTabSpec("选项三") .setIndicator("选项三", getResources().getDrawable(R.drawable.icon)) .setContent(R.id.view3)); }}
- Android之TabHost的几种使用方法
- Android之TabHost的几种使用方法
- TabHost的两种使用方法
- TabHost的两种使用方法
- android TabHost(选项卡)的使用方法发布
- android TabHost(选项卡)的使用方法
- android TabHost(选项卡)的使用方法
- android tabhost使用方法
- android TabHost使用方法
- android:TabHost使用方法
- android:Tabhost使用方法
- Android之TabHost的创建
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- TabWidget/TabHost的两种使用方法
- TabHost的使用方法
- Session过滤器
- 据说看完这21个故事的人,30岁前都成了亿万富翁。你是下一个吗?
- vi快捷键
- 芯故事 心感动:英特尔企业文化的力量
- java 读取配置文件(一)
- Android之TabHost的几种使用方法
- 从两道经典试题谈C/C++中联合体(union)的使用
- QThread 使用探讨(zz)
- UIWebView显示gif图片
- Ogre水波演示Demo(注释)
- 视频截图
- java 读取配置文件(二)
- tomcat相对路径
- 如何在oracle中导入导出dmp数据库文件