TabWidget/TabHost的两种使用方法
2015-11-30 11:39
441 查看
Android TabWidget/TabHost有两种使用方法:
第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。
【Android进阶】嵌套TabHost (TabHost中放TabHost,类似二级目录、二级树)
http://blog.csdn.net/feng88724/archive/2011/02/23/6203358.aspx
转自:http://gundumw100.iteye.com/blog/853967
第一种:使用系统自带写好的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>
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>
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进阶】嵌套TabHost (TabHost中放TabHost,类似二级目录、二级树)
http://blog.csdn.net/feng88724/archive/2011/02/23/6203358.aspx
转自:http://gundumw100.iteye.com/blog/853967
相关文章推荐
- -webkit-box 与 -webkit-flex 的差异
- github 如何合并不同分支
- matlab命令,应该很全了!
- 当一回Android Studio 2.0的小白鼠
- html file选中图片后 不经过服务器 立刻显示在页面
- bzoj3687: 简单题
- 有时$_SERVER["HTTP_REFERER"]为空的原因
- Android实现全屏显示几种方法
- 华中师范大学 ACM 协会博客
- 卷积神经网络在自然语言处理的应用
- 我也要谈谈大型网站架构之系列(4)——分布式中的异步通信
- KVM 介绍:libvirt 介绍
- 查看alter错误,grep -A,-B,-C的妙用
- MediaStore.Images.Media.insertImage 得到保存图片的原始路径
- VS2010调用opencv2.4(64位)
- Linux配置JDK和tomcat
- Spring事务管理的三种方式
- PHP获取远程图片并保存到本地
- 在eclipse搭建python开发环境
- spring解决中文乱码问题