本文主要是介绍TabHost的两种使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android 实现tab视图有2种方法,一种就是继承tabactivity,一种是在布局页面中定义<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>
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"> <FrameLayout android:id="@android:id/tabcontent" //这里的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> <TabWidget android:id="@android:id/tabs" //注意id表述方式,一定要是"@android:id/tabs"android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout> </TabHost>
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()); } }
}
PS
1、android中的TabActivity是专门用来放置TabHost组件的,但是很遗憾,非常固定,必须放在顶部,对于单手使用手机的人来说,很不方便。所以研究了一下如何将TabBar放置底部,具体思路是在xml布局文件中,将TabWidget放于FrameLayout的下面(两者是垂直线性的关系),并分配各个组件的权重。另外,如果想去掉Tab中那个丑陋的白线,可以通过设置其Padding实现。
2、让自己的类继承TabActivity,然后通过调用getTabHost()方法得到tabhost对象,然后把自己写好的数据展示的布局文件加载到tabhost中,就可以实现了。最后是通过调用addTab()方法添加标签的相关属性(如:标签名称,标签图片,标签内容布局)。
而如果通过XML文件配置tabHost则需要注意的是,framelayout,tabwidget标签的id都必须引用系统的id(@android:id/tabcontent,@android:id/tabs),不然会报异常.在程序用使用findViewById()加载tabhost,然后调用tabhost.setup()方法初始化tabhost,后面的步骤则和上面一种一样
这篇关于TabHost的两种使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!