隐藏tabwidgt 用radiogroup来实现tab的切换
(2011-12-15 15:31:36)
Java代码:
package com.studio.test.mm;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class MainTabUI extends TabActivity {
publicstatic final String TAB_WENXIN = "微信";
publicstatic final String TAB_ADRESS = "通讯录";
publicstatic final String TAB_FRIENDS = "找朋友";
publicstatic final String TAB_SETTING = "设置";
privateTabHost tabHost=null;
privateTabHost.TabSpec spec=null;
privateRadioGroup tab_group;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_radiogroup);
tabHost=getTabHost();//获取系统的tabHost对象
spec=tabHost.newTabSpec(TAB_WENXIN);
spec.setContent(R.id.txt1).setIndicator(TAB_WENXIN);
tabHost.addTab(spec);
spec=tabHost.newTabSpec(TAB_ADRESS);
spec.setContent(R.id.txt2).setIndicator(TAB_ADRESS);
tabHost.addTab(spec);
spec=tabHost.newTabSpec(TAB_FRIENDS);
spec.setContent(R.id.txt3).setIndicator(TAB_FRIENDS);
tabHost.addTab(spec);
spec=tabHost.newTabSpec(TAB_SETTING);
spec.setContent(R.id.txt4).setIndicator(TAB_SETTING);
tabHost.addTab(spec);
setDefaultTab(0);//设置默认的内容页
tab_group=(RadioGroup)findViewById(R.id.main_tab_group);
tab_group.setOnCheckedChangeListen
er(new OnCheckedChangeListener(){
@Override
public voidonCheckedChanged(RadioGroup group, int checkedId) {
//checkedId指当前选中的RadioButton的ID,等于group.getCheckedRadioButtonId().
switch(checkedId) {
caseR.id.main_tab_weixin:
tabHost.setCurrentTabByTag(TAB_WENXIN);//设置当前TabHost内容页
break;
caseR.id.main_tab_address:
tabHost.setCurrentTabByTag(TAB_ADRESS);
break;
caseR.id.main_tab_find_friend:
tabHost.setCurrentTabByTag(TAB_FRIENDS);
break;
caseR.id.main_tab_settings:
tabHost.setCurrentTabByTag(TAB_SETTING);
break;
default:
break;
}
}
});
}
}
最关键就是下面这个XML了。它调用系统的tabhost。
布局文件:
<?xml version="1.0"encoding="utf-8"?>
<!--微信 通讯录 找朋友 设置
底部选项卡-->
<TabHostandroid:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<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="0.0dip"android:layout_weight="1.0" >
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button 1"
android:textSize="36sp"
android:textColor="#4a9ad8"/>
<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button 2"
android:textSize="36sp"
android:textColor="#4a9ad8"/>
<TextView
android:id="@+id/txt3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button 3"
android:textSize="36sp"
android:textColor="#4a9ad8"/>
<TextView
android:id="@+id/txt4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Button 4"
android:textSize="36sp"
android:textColor="#4a9ad8"/>
</FrameLayout>
<TabWidgetandroid:id="@android:id/tabs"android:visibility="gone" android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_weight="0.0"/>
<RadioGroup android:gravity="bottom"android:layout_gravity="bottom" android:orientation="horizontal"android:id="@id/main_tab_group"android:background="@drawable/mmfooter_bg"android:paddingTop="2.0dip" android:layout_width="fill_parent"android:layout_height="wrap_content">
<RadioButton android:id="@id/main_tab_weixin"android:checked="true" android:text="@string/main_title"android:drawableTop="@drawable/tab_weixin"style="@style/MMTabButton" />
<RadioButton android:id="@id/main_tab_address"android:text="@string/main_contact"android:drawableTop="@drawable/tab_address"style="@style/MMTabButton" />
<RadioButton android:id="@id/main_tab_find_friend"android:text="@string/main_addcontact"android:drawableTop="@drawable/tab_find_frd"style="@style/MMTabButton" />
<RadioButton android:id="@id/main_tab_settings"android:text="@string/main_setting"
android:drawableTop="@drawable/tab_settings"style="@style/MMTabButton"/>
</RadioGroup>
</LinearLayout>
</TabHost>
样式:
<!-- Tab选项标签 -->
<style name="MMTabButton">
<itemname="android:textSize">12.0dip</item>
<itemname="android:textColor">@color/mm_tab_btn</item>
<itemname="android:gravity">center_horizontal</item>
<itemname="android:background">@drawable/tab_bg</item>
<itemname="android:layout_width">0.0dip</item>
<itemname="android:layout_height">wrap_content</item>
<itemname="android:layout_marginLeft">0.70000005dip</item>
<itemname="android:layout_marginRight">0.70000005dip</item>
<itemname="android:button">@null</item>
<itemname="android:layout_weight">1.0</item>
</style>
样式中比较重要的两个属性:
android:button="@null"——去掉RadioButton中的Button按钮(只保留文字)
android:drawableTop="@drawable/tab_settings"——在文字上方显示图片