- 浏览: 121721 次
- 性别:
- 来自: 深圳
最新评论
-
T_bag:
...
TabHost 中的Activity执行顺序 -
ihopethatwell:
楼主,你能否写一个 int类型的一维数组的结构体?
linux NDK实例 -
gf_crazy:
刚好找第二种,其他地方全是第一种。
TabHost -
gangbener:
我们是可以把不同分辨率的图片放到不同的图片文件夹中去,问题是: ...
android程序中屏幕问题解决方案 -
shusanzhan:
学习了,Mark
android应用收费渠道
Android TabWidget/TabHost有两种使用方法:
第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/tab1"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView1"
- android:text="This is a tab1" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/tab2"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView2"
- android:text="This is a tab2" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/tab3"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextView android:id="@+id/TextView3"
- android:text="This is a tab3" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <TextView android:id="@+id/TextView1" android:text="This is a tab1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <TextView android:id="@+id/TextView2" android:text="This is a tab2" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <TextView android:id="@+id/TextView3" android:text="This is a tab3" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> </FrameLayout>
- package com.Aina.Android;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.TabActivity;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- public class Test_TabWidget extends TabActivity {
- /** Called when the activity is first created. */
- private TabHost tabHost;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
- tabHost = this.getTabHost();
- LayoutInflater li = LayoutInflater.from(this);
- li.inflate(R.layout.main, tabHost.getTabContentView(), true);
- tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)
- .setIndicator("TAB1",
- this.getResources().getDrawable(R.drawable.img1)));
- tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)
- .setIndicator("TAB2",
- this.getResources().getDrawable(R.drawable.img2)));
- tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)
- .setIndicator("TAB3",
- this.getResources().getDrawable(R.drawable.img3)));
- tabHost.setCurrentTab(1);
- // tabHost.setBackgroundColor(Color.GRAY);
- tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
- public void onTabChanged(String tabId) {
- Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)
- .setTitle("提示").setMessage(
- "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){
- public void onClick(DialogInterface dialog,
- int which) {
- // TODO Auto-generated method stub
- }
- }).create();
- dialog.show();
- }
- });
- }
- }
package com.Aina.Android; import android.app.AlertDialog; import android.app.Dialog; import android.app.TabActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class Test_TabWidget extends TabActivity { /** Called when the activity is first created. */ private TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); tabHost = this.getTabHost(); LayoutInflater li = LayoutInflater.from(this); li.inflate(R.layout.main, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1) .setIndicator("TAB1", this.getResources().getDrawable(R.drawable.img1))); tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2) .setIndicator("TAB2", this.getResources().getDrawable(R.drawable.img2))); tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3) .setIndicator("TAB3", this.getResources().getDrawable(R.drawable.img3))); tabHost.setCurrentTab(1); // tabHost.setBackgroundColor(Color.GRAY); tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { public void onTabChanged(String tabId) { Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this) .setTitle("提示").setMessage( "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); } }); } }
第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/TabHost01" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:layout_width="fill_parent"
- android:orientation="vertical" 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">
- <LinearLayout android:id="@+id/LinearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextView android:text="one"
- android:id="@+id/TextView01" android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/LinearLayout2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="two"
- android:id="@+id/TextView02" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayout android:id="@+id/LinearLayout3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="three"
- android:id="@+id/TextView03" android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TabHost01" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" 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"> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="one" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="two" android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="three" android:id="@+id/TextView03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
- package com.Aina.Android;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.widget.TabHost;
- public class Test_TabHost extends Activity {
- /** Called when the activity is first created. */
- private TabHost tabHost;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- try{
- tabHost = (TabHost) this.findViewById(R.id.TabHost01);
- tabHost.setup();
- tabHost.addTab(tabHost.newTabSpec("tab_1")
- .setContent(R.id.LinearLayout1)
- .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));
- tabHost.addTab(tabHost.newTabSpec("tab_2")
- .setContent(R.id.LinearLayout2).setIndicator("TAB2",
- this.getResources().getDrawable(R.drawable.img2)));
- tabHost.addTab(tabHost.newTabSpec("tab_3")
- .setContent(R.id.LinearLayout3).setIndicator("TAB3",
- this.getResources().getDrawable(R.drawable.img3)));
- tabHost.setCurrentTab(1);
- }catch(Exception ex){
- ex.printStackTrace();
- Log.d("EXCEPTION", ex.getMessage());
- }
- }
- }
package com.Aina.Android; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.widget.TabHost; public class Test_TabHost extends Activity { /** Called when the activity is first created. */ private TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try{ tabHost = (TabHost) this.findViewById(R.id.TabHost01); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab_1") .setContent(R.id.LinearLayout1) .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1))); tabHost.addTab(tabHost.newTabSpec("tab_2") .setContent(R.id.LinearLayout2).setIndicator("TAB2", this.getResources().getDrawable(R.drawable.img2))); tabHost.addTab(tabHost.newTabSpec("tab_3") .setContent(R.id.LinearLayout3).setIndicator("TAB3", this.getResources().getDrawable(R.drawable.img3))); tabHost.setCurrentTab(1); }catch(Exception ex){ ex.printStackTrace(); Log.d("EXCEPTION", ex.getMessage()); } } }
注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。
发表评论
-
android.os.NetworkOnMainThreadException
2011-12-24 13:14 1183不能在android的主线程中,执行一个网络操作 ... -
转载:为什么要对URI进行编码
2011-12-15 15:49 1003为什么需要Url编码,通常如果一样东西需要编码,说明这样东 ... -
multipart form-data boundary
2011-12-15 15:23 958含义 ENCTYPE="multipart/ ... -
android http 附件
2011-12-15 14:17 1589一:服务器端: 1:struts-config.xml ... -
post 附件
2011-12-15 10:24 953在做嘀咕客户端的时候,要实现拍照上传的功能。根据嘀咕api ... -
让Android应用获取系统权限
2011-12-08 18:46 963在 android 的API中有提供 SystemCloc ... -
Android源码目录结构详解
2011-12-01 20:22 777Android 2.1 |-- Makefile |-- ... -
两个activity跳转
2011-11-25 16:06 1221Activity A跳转到 Activity B /**A. ... -
线程同步之wait()/notify()的使用
2011-11-21 11:24 931wait()/notify() 通常,多 ... -
游戏中渲染线程与更新线程交替执行
2011-11-21 11:21 886private final State mThreadLock ... -
android colormatrix
2011-11-03 17:32 1454在编程中有时候需要 ... -
java栈,堆,池
2011-07-08 09:38 701今天复习了一下这些知识,顺便做了下笔记.1.寄存器:最快的存储 ... -
3D开发的境界
2011-06-04 20:12 683第一阶段:初学者阶 ... -
Http
2011-06-01 17:10 1266使用 HTTP 服务: 1. Apache HttpCline ... -
获取手机的Opengl的支持版本
2011-05-27 09:28 1438public int getGLVersion() { ... -
性能优化
2011-05-27 09:26 758如果你想写一个 Java 程序,观察某对象什么时候会被垃圾收集 ... -
Android游戏中其他语言数据类型之间的转换方法
2011-05-17 11:43 1181Java与其他语言数据类型之间的转换方法实例程序 /* ... -
android canvas.getClipBounds
2011-05-13 17:41 8393一种是传参数: Rect dstRect = new Re ... -
获取屏幕大小的方法
2011-05-13 17:38 566// one DisplayMetrics dm = n ... -
Android Lock 使用
2011-05-13 16:43 3142PowerManager 和PowerManager.Wa ...
相关推荐
现今很流行的 TabHost的各种实现方式的代码
TabHost中填充自定义ListView
tabhost中嵌套一个tabhost。
TabHost的使用方法,适合初学者,适合课堂讲授实现模拟显示未接来电、已接来电和拨出电话的选项卡。
TabHost+ViewPager实现滑动tabhost, viewPager;tabhost;滑动;源码
Android 嵌套TabHost示例. 两层TabHost嵌套,实现的效果类似网页上的二级菜单栏。
tabhost标签页面简单实现,这是学习android初期做的demo,
tabhost页面跳转需要用到的tabhost单例模式小例子,也有tabhost底部的布局
Android TabHost TabActivity TabHost的简单使用
Android TabHost实现屏幕UI窗口的TAB切换功能,实现的过程:声明TabHost对象、取得TabHost对象,为TabHost添加标签,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),然后...
ViewPager和Tabhost结合,可滑动的tabhost源代码,是一个很不错的Android源码,有兴趣的伙伴们抽时间可以看一下把
TabHost分页简单应用,以及解决TabHost下面白线问题.
android Tabhost使用Demo
1、底部tabhost嵌套顶部tabhost. 2、顶部tabhost,嵌套listview。 3、顶部tabhost子tab选项仿网易新闻功能,可以滑动,每一个对应一个listvie
Android TabHost TabWidget 切换卡
android-->tabHost 源码
androidTabhost的使用实例代码,仅供参考
TabHost实现底部菜单, 底部菜单选项卡主要是通过TabHost和RadioGroup实现
android TabHost(标签)的使用 简单实例。
TabHost布局的框架,点击button按钮切换页面、随即更改字体颜色和背景图片。