Android(安卓)TabWidget/TabHost的使用
16lz
2021-12-04
2010 - 06 - 07
第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
Java代码 ⃕
Java代码 ⃕
第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:
Java代码 ⃕
Java代码 ⃕
注意:第二种方法时布局文件中的TabWidget的id必须定义 为:android:id="@android:id/tabs",FrameLayout的id必须定义 为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。
Android TabWidget/TabHost的使用
文章分类:Java编程 Android TabWidget/TabHost有两种使用方法:第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
Java代码 ⃕
- <?xmlversion="1.0"encoding="utf-8"?>
- <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayoutandroid:id="@+id/tab1"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextViewandroid:id="@+id/TextView1"
- android:text="Thisisatab1"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayoutandroid:id="@+id/tab2"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextViewandroid:id="@+id/TextView2"
- android:text="Thisisatab2"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayoutandroid:id="@+id/tab3"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- androidrientation="vertical">
- <TextViewandroid:id="@+id/TextView3"
- android:text="Thisisatab3"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
Java代码 ⃕
- packagecom.Aina.Android;
- importandroid.app.AlertDialog;
- importandroid.app.Dialog;
- importandroid.app.TabActivity;
- importandroid.content.DialogInterface;
- importandroid.os.Bundle;
- importandroid.view.LayoutInflater;
- importandroid.widget.TabHost;
- publicclassTest_TabWidgetextendsTabActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateTabHosttabHost;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.main);
- tabHost=this.getTabHost();
- LayoutInflaterli=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(newTabHost.OnTabChangeListener(){
- publicvoidonTabChanged(StringtabId){
- Dialogdialog=newAlertDialog.Builder(Test_TabWidget.this)
- .setTitle("提示").setMessage(
- "选中了"+tabId+"选项卡").setIcon(R.drawable.icon).setPositiveButton("确定",newDialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,
- intwhich){
- //TODOAuto-generatedmethodstub
- }
- }).create();
- dialog.show();
- }
- });
- }
- }
第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:
Java代码 ⃕
- <?xmlversion="1.0"encoding="utf-8"?>
- <TabHostxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/TabHost01"android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayoutandroid:layout_width="fill_parent"
- android:orientation="vertical"android:layout_height="fill_parent">
- <TabWidgetandroid:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <FrameLayoutandroid:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayoutandroid:id="@+id/LinearLayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <TextViewandroid:text="one"
- android:id="@+id/TextView01"android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayoutandroid:id="@+id/LinearLayout2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextViewandroid:text="two"
- android:id="@+id/TextView02"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- <LinearLayoutandroid:id="@+id/LinearLayout3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextViewandroid:text="three"
- android:id="@+id/TextView03"android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
Java代码 ⃕
- packagecom.Aina.Android;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.LayoutInflater;
- importandroid.widget.TabHost;
- publicclassTest_TabHostextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateTabHosttabHost;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- 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(Exceptionex){
- ex.printStackTrace();
- Log.d("EXCEPTION",ex.getMessage());
- }
- }
- }
注意:第二种方法时布局文件中的TabWidget的id必须定义 为:android:id="@android:id/tabs",FrameLayout的id必须定义 为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。
更多相关文章
- android中自定义Theme以及TitleBar
- drawable中的layer-list使用
- Android(安卓)近百个项目的源代码,覆盖Android开发的每个领域
- Android里把Dialog设置为全屏的方法
- android 显示特殊符号
- android 中调用接口发送短信
- 【Android代码片段之四】设置全屏
- android 中调用接口发送短信
- Android(安卓)MVVM结合DataBinding的简单实用(Android(安卓)studi