TabHost结合RadioButton实现主页的导航效果

时间:2025-01-18 18:06:38
 布局文件的设置,如下

 <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhosts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_weight="1.0"
android:layout_height="0.0dp" /> <TabWidget
android:visibility="gone"
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal"/> <RadioGroup
android:layout_gravity="bottom"
android:gravity="center"
android:id="@+id/main_rdg"
android:background="@drawable/main_radio_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <RadioButton
style="@style/bottom_radio_style"
android:id="@+id/rd_index"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="@string/index"
android:gravity="center_horizontal"
android:drawableTop="@drawable/icon_tab_homepage_checked"
android:layout_weight="1"
/> <RadioButton
style="@style/bottom_radio_style"
android:id="@+id/rd_near"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/near"
android:drawableTop="@drawable/icon_tab_nearby_checked" /> <RadioButton
style="@style/bottom_radio_style"
android:id="@+id/rd_own"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/own"
android:drawableTop="@drawable/icon_tab_mine_checked"/> <RadioButton
style="@style/bottom_radio_style"
android:id="@+id/rd_more"
android:text="@string/more"
android:drawableTop="@drawable/icon_tab_more_checked"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</RadioGroup> </LinearLayout>
</TabHost> 通过样式文件更改radioButton的效果 <?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="bottom_radio_style"> <item name="android:button">@null</item> <item name="android:gravity">center</item> <item name="android:background">@drawable/main_bottom_rd</item> </style> </resources> 选择效果(选择一个标签是的一个选择效果) <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/main_radio_select"></item>   <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/main_radio_select"></item> </selector> activity中的代码设置如下: package cn.liu.activity;
public class MainActivity extends ActivityGroup {
protected static final String TAG = "MainActivity";
private TabHost tabHost = null;
private RadioGroup rGroup; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置手机全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
// 通过findviewbyId这种方式找到tabhost,就必须要调用
// setup方法,而且如果当前类,没有集成ActivityGroup,会报E/AndroidRuntime(380): java.lang.RuntimeException: Unable to start activity
       ComponentInfo{cn.liu.eat/cn.liu.activity.MainActivity}: java.lang.IllegalStateException: Did you forget to call 'public void
setup(LocalActivityManager activityGroup)且setup必须要设置成this.getLocalActivityManager()这样才不会出问题
tabHost = (TabHost) this.findViewById(R.id.tabhosts);
tabHost.setup(this.getLocalActivityManager());
TabSpec tabSpec1 = tabHost.newTabSpec("tab1").setIndicator("tab1")
.setContent(new Intent(this, HomeActivity.class));
TabSpec tabSpec2 = tabHost.newTabSpec("tab2").setIndicator("tab2")
.setContent(new Intent(this, NearActivity.class));
TabSpec tabSpec3 = tabHost.newTabSpec("tab3").setIndicator("tab3")
.setContent(new Intent(this, MineActivity.class));
TabSpec tabSpec4 = tabHost.newTabSpec("tab4").setIndicator("tab4")
.setContent(new Intent(this, MoreActivity.class)); tabHost.addTab(tabSpec1);
tabHost.addTab(tabSpec2);
tabHost.addTab(tabSpec3);
tabHost.addTab(tabSpec4);
tabHost.setCurrentTab(0); rGroup = (RadioGroup) this.findViewById(R.id.main_rdg);
rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rd_index:
tabHost.setCurrentTabByTag("tab1");
break;
case R.id.rd_near:
tabHost.setCurrentTabByTag("tab2");
break;
case R.id.rd_own:
tabHost.setCurrentTabByTag("tab3");
break;
case R.id.rd_more:
tabHost.setCurrentTabByTag("tab4");
break;
}
}
}); } }