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>
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_TabWidget选项卡总结
Android TabHost TabWidget 切换卡
TabHost+TabWidget+FrameLayout实现主页面实现底部栏;
TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。 先来贴出本例运行的截图: main.xml的源码如下: <?xml version=1.0 encoding=utf-8?> <TabHost android:layout_width=...
<TabHost android:id="@android:id/tabhost" tools:context="com.example.ydc.filemanager.MainActivity"> <LinearLayout android:background="@drawable/login_bg" android:orientation="vertical"> <TabWidget ...
先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 ...本次学习效果图: ...第一,主布局文件(启动页main.xml,位于res/layout目录下)代码 ...xml version=....TabHost xmlns:android=...android:id=@+id/tabhost
本文实例讲述了Android编程设置TabHost当中字体的方法。分享给大家供大家参考,具体如下: TabWidget tw=this.getTabWidget();//设置TabHost当中的内容的方法 for(int i=0;i<tw.getChildCount();i++){ //设置...
http://blog.csdn.net/birdno1/article/details/50516725 通过自定义view解决tabwidget下划线颜色问题
TabHost控件默认使用LinearLayout包裹TabWidget和FrameLayout,布局文件如下: <TabHost xmlns:android=http://schemas.android.com/apk/res/android android:id=@android:id/tabhost android:layout_width=match...
Android TabHost示例使用XML TabHost和TabWidget的Android Tab示例。 单击下面的链接以获取教程。演示版![ALT文本( “可选标题”)
1:TabHost 、TabWidget、FrameLayout一定添加id这个属性,否则会报错 android:id=”@android:id/tabhost” android:id=”@android:id/tabcontent” android:id=”@android:id/tabs” 这个属性是固定的。 2...
自定义Android中TabHost组件显示在屏幕底部,并实现滑动切换页面。在此基础上可以实现类似米聊等软件主界面的Tab效果。
Android入门第十一篇之TabHost,TabWidget.docx
TabHost、TabWidget 、FrameLayout 如果继承TabActivity,并且通过getTabHost()方法来获取TabHost,那么三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent,如果继承Activity,...
本文实例讲述了Android切换卡TabWidget用法。分享给大家供大家参考,具体如下: Tab选项卡类似与电话本的界面,通过多个标签切换不同的内容,要实现这个效果,首先要知道TabHost,它是一个用来存放多个Tab标签的容器...
本文实例讲述了Android编程之TabWidget选项卡用法。分享给大家供大家参考,具体如下: 1 概览 TabWidget与TabHost。tab组件一般包括TabHost和TabWidget、FrameLayout,且TabWidget、FrameLayout属于TabHost。 是否...
TabWidget类似于Android 中查看电话薄的界面,通过多个标签切换显示不同内容。要实现这一效果,首先要了解TabHost,它是一个用来存放多个Tab标签的容器。每一个Tab都可以对应自己的布局,比如,电话薄中的Tab布局...
TabHost组件可以在界面中存放多个选项卡, 很多软件都使用了改组件进行设计。 一、基础知识 TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡; TabSpec : 代表了选项卡界面, 添加...