android tabhost之RadioGroup
1:布局文件
<?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" >
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<FrameLayout android:id="@+id/msg_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" >
<TextView android:id="@+id/tv_wb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/titlebar_lightgray_bg" > </TextView> </FrameLayout>
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" />
<TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone" />
<RadioGroup android:id="@id/main_radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/maintab_toolbar_bg" android:gravity="center_vertical" android:orientation="horizontal" >
<RadioButton android:id="@+id/radio_button0" style="@style/main_tab_bottom" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" android:tag="radio_button0" android:text="@string/main_home" />
<RadioButton android:id="@+id/radio_button1" style="@style/main_tab_bottom" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_2_n" android:tag="radio_button1" android:text="@string/main_news" />
<RadioButton android:id="@+id/radio_button2" style="@style/main_tab_bottom" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_3_n" android:tag="radio_button2" android:text="@string/main_my_info" />
<RadioButton android:id="@+id/radio_button3" style="@style/main_tab_bottom" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_4_n" android:tag="radio_button3" android:text="@string/menu_search" />
<RadioButton android:id="@+id/radio_button4" style="@style/main_tab_bottom" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_5_n" android:tag="radio_button4" android:text="@string/more" /> </RadioGroup> </LinearLayout>
</TabHost>
2:RadioButton的style="@style/main_tab_bottom"文件
<style name="main_tab_bottom">
<item name="android:textSize">10.0dip</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">5.0dip</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">2.0dip</item>
<item name="android:layout_weight">1.0</item>
</style>
3:<item name="android:background">@drawable/home_btn_bg</item> 的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
</selector>
4:主类里的调用代码:(class MainActivity extends TabActivity):
public TabHost mth;
public static final String TAB_HOME="首页";
public static final String TAB_MSG="信息";
//----------------------------------
mth=this.getTabHost();
TabSpec ts1=mth.newTabSpec(TAB_HOME).setIndicator(TAB_HOME);
ts1.setContent(new Intent(MainActivity.this,FirstActivity.class));
mth.addTab(ts1);
TabSpec ts2=mth.newTabSpec(TAB_MSG).setIndicator(TAB_MSG);
ts2.setContent(new Intent(MainActivity.this,SecondActivity.class));
mth.addTab(ts2);
this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.radio_button0:
mth.setCurrentTabByTag(TAB_HOME);
break;
case R.id.radio_button1:
mth.setCurrentTabByTag(TAB_MSG);
break;
case R.id.radio_button2:
mth.setCurrentTabByTag(TAB_HOME);
break;
case R.id.radio_button3:
mth.setCurrentTabByTag(TAB_MSG);
break;
case R.id.radio_button4:
mth.setCurrentTabByTag(TAB_HOME);
break;
}
}
});
posted on 2013-05-31 15:42 yujian_bcq 阅读(208) 评论(0) 编辑 收藏 举报