今天简单总结了下TabHost,总体来说,有三种形式(现在为止只发现这3种)。
首先要了解一般的结构
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost" >
<TabWidget 相当于几个选项卡
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<FrameLayout 每个选项卡的内容
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</FrameLayout>
</TabHost>
注意id必须写成红字的形式
第一种:那就是用LayoutInflater类,LayoutInflater这个类的作用类似于findViewById(),不同点: LayoutInflater是用来找layout下xml布局文件的,而且它会实例化,
findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮。inflate就相当于将一个xml中定义的布局找出来.因为如果在一个Activity文件里直接用
findViewById()这个方法的话,那么它所对应的是setConentView()中调用的那个layout里的组件. 因此如果在同样的Activity里用到别的layout的话,而且你还要设置这个
layout里的组件(比如:ImageView,TextView)上的内容,那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件,然后进行一系列的操作
,inflate()方法中参数:1.想要用的布局文件的id 2.持有选项卡的内容,获取FrameLayout 3.true:将此处解析的xml文件做为根视图View。
代码:
public class TabHostActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabhost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabhost_layout,
tabhost.getTabContentView(), true);
/**在这里添加的时候:
* 1.必须指定 tab 的内容,必须为 id, 即:setContent(R.id.text)
* 2.必须设置tab 上的文字或图片 , 即:setIndicator("已接电话")
* 3.返回一个 TabHost.TabSpec 对象,其参数用于标识一个 tab 的 tag,即:newTabSpec("tab1")
*/
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已接电话")
.setContent(R.id.text1));
tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("呼出电话",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.text2));
tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("未接电话")
.setContent(R.id.text3));
布局文件只需用一个,就可以达到一般选项卡的效果
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/first_Tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="1"></TextView>
<TextView
android:id="@+id/second_Tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="2">></TextView>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
</TabHost>
第二种形式,main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"> 利用相对布局,设置左面红色所示的属性,可以让TabWidget在 下面。类似于一些软件,如微博中的效果。
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
</RelativeLayout>
</TabHost>
TabNormalActivity.java
package wc.tab.test;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabNormalActivity extends TabActivity {
private TabHost tabhost=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabhost = getTabHost();
tabhost.addTab(tabhost.newTabSpec("111")
.setIndicator("TAB1")
.setContent(new Intent(this,FirstActivity.class)));
tabhost.addTab(tabhost.newTabSpec("222")
.setContent(new Intent(this,SecondActivity.class))
.setIndicator("TAB2"));
}
}
注意个选项卡都是一个Activity,所以都有它们对应的布局。
第三种形式,就是用其他控件替换TabWidget,比如RadioButton等。注意,如果想用控件代替TabWidget,那么就要把TabWidget的visibility设置为gone,也就是说不可见。
部分代码:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone" >
</TabWidget>
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="@drawable/maintab_toolbar_bg"
android:gravity="center_vertical" >
<RadioButton
android:id="@+id/radio_button0"
android:drawableTop="@drawable/icon_1_n"
android:tag="radio_button0"
style="@style/main_tab_bottom"
android:text="@string/main_home" />
<RadioButton
android:id="@+id/radio_button1"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/icon_2_n"
android:tag="radio_button1"
style="@style/main_tab_bottom"
android:text="@string/main_message" />
<RadioButton
android:id="@+id/radio_button2"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/icon_3_n"
android:tag="radio_button2"
style="@style/main_tab_bottom"
android:text="@string/main_friends" />
<RadioButton
android:id="@+id/radio_button3"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/icon_4_n"
android:tag="radio_button3"
style="@style/main_tab_bottom"
android:text="@string/main_square" />
<RadioButton
android:id="@+id/radio_button4"
android:layout_marginTop="2dp"
android:drawableTop="@drawable/icon_5_n"
android:tag="radio_button4"
style="@style/main_tab_bottom"
android:text="@string/main_other" />
</RadioGroup>
</LinearLayout>
</TabHost>
MainActivity.ava
package wc.weibo.two;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
private static final String TAG="TEST";
private TabHost tabHost=null;
private RadioGroup radioGroup=null;
private Intent intent=null;
private TabSpec spec=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTabSpec();
changeTab();
}
//初始化并设置每个选项卡
public void setTabSpec(){
tabHost=this.getTabHost();
//首页选项卡
intent=new Intent("weibo.home");
spec=tabHost.newTabSpec("首页").setIndicator("首页").setContent(intent);
tabHost.addTab(spec);
//消息选项卡
intent=new Intent("weibo.message");
spec=tabHost.newTabSpec("消息").setIndicator("消息").setContent(intent);
tabHost.addTab(spec);
//好友选项卡
intent=new Intent("weibo.friend");
spec=tabHost.newTabSpec("好友").setIndicator("好友").setContent(intent);
tabHost.addTab(spec);
//广场选项卡
intent=new Intent("weibo.square");
spec=tabHost.newTabSpec("广场").setIndicator("广场").setContent(intent);
tabHost.addTab(spec);
//更多选项卡
intent=new Intent("weibo.other");
spec=tabHost.newTabSpec("更多").setIndicator("更多").setContent(intent);
tabHost.addTab(spec);
}
//根据ID获取单选按钮组,并进行监听
public void changeTab(){
radioGroup=(RadioGroup)findViewById(R.id.main_radio);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio_button0:
tabHost.setCurrentTabByTag("首页");
Log.d(TAG, "首页");
break;
case R.id.radio_button1:
tabHost.setCurrentTabByTag("消息");
Log.d(TAG, "2");
break;
case R.id.radio_button2:
tabHost.setCurrentTabByTag("好友");
Log.d(TAG, "3");
break;
case R.id.radio_button3:
tabHost.setCurrentTabByTag("广场");
Log.d(TAG, "4");
break;
case R.id.radio_button4:
tabHost.setCurrentTabByTag("更多");
Log.d(TAG, "5");
break;
default:
tabHost.setCurrentTabByTag("首页");
break;
}
}
});
}
}
以上就是我总结的一部分,希望对你们有帮助,如果有什么地方说的不对的,请与我联系。谢谢