效果:
这样做,等于是自定义了tabhost的tab按钮。
部分代码:
xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- //头部 --> <include layout="@layout/activity_base_head" /> <RelativeLayout android:id="@id/content" android:layout_below="@id/add_title" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RadioGroup android:background="@android:color/black" android:id="@+id/btnsGroup" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="7dp" android:layout_marginRight="7dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp"> <!-- baike --> <RadioButton android:id="@+id/baike" android:text="百科" android:layout_width="wrap_content" android:layout_height="40px" android:textSize="16sp" android:layout_weight="1" android:gravity="center_horizontal" android:background="@drawable/radio_bg_selector" android:button="@null" /> <!-- zhidao --> <RadioButton android:id="@+id/zhidao" android:text="知道" android:layout_marginLeft="7dp" android:layout_width="wrap_content" android:layout_height="40px" android:textSize="16sp" android:layout_weight="1" android:gravity="center_horizontal" android:button="@null" android:background="@drawable/radio_bg_selector"/> <!-- xinwen --> <RadioButton android:id="@+id/xinwen" android:text="新闻" android:layout_marginLeft="7dp" android:layout_width="wrap_content" android:layout_height="40px" android:textSize="16sp" android:layout_weight="1" android:gravity="center_horizontal" android:button="@null" android:background="@drawable/radio_bg_selector" /> </RadioGroup> </RelativeLayout> </RelativeLayout>
groups = (RadioGroup)findViewById(R.id.btnsGroup); groups.setOnCheckedChangeListener(new CheckedChangeListener()); container = (LinearLayout) findViewById(R.id.containerBody); groups.check(R.id.baike);//选择一个按钮
class CheckedChangeListener implements RadioGroup.OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { View rootView=null; switch (checkedId) { case R.id.baike: rootView=getLocalActivityManager().startActivity( "Module3", new Intent(SearchActivity.this, BaikeActivity.class).putExtra(Config.ACTIVITY_KEY_WORD, keyWord) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); break; case R.id.zhidao: rootView=getLocalActivityManager().startActivity( "Module2", new Intent(SearchActivity.this, ZhidaoActivity.class).putExtra(Config.ACTIVITY_KEY_WORD, keyWord) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); break; case R.id.xinwen: rootView=getLocalActivityManager().startActivity( "Module1", new Intent(SearchActivity.this, NewsActivity.class).putExtra(Config.ACTIVITY_KEY_WORD, keyWord) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); break; default: break; } container.removeAllViews(); container.addView(rootView); }
radio_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/radio_checked_bg"/> <!-- pressed --> <item android:drawable="@drawable/radio_bg"/> <!-- default --> </selector>