android TabHost简单使用
TabHost配合Radio按钮 简单使用
使用TabHost 可以在一个屏幕间进行不同版面的切换
一、继承TabActivity
示例
public class MainActivity extends TabActivity {
private TabHost tabHost;
private RadioGroup radioGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup)findViewById(R.id.main_radio);
tabHost = getTabHost();
//添加选项卡
tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("TAB ONE").setContent(new Intent(this,TaboneActivity.class)));
tabHost.addTab(tabHost.newTabSpec("tab_test2").setIndicator("TAB_TWO").setContent(new Intent(this, TabtwoActivity.class)));
//初始选中
tabHost.setCurrentTabByTag("tab_test2");
//设置为默认选中
radioGroup.check(R.id.strollbtn);
//radio按钮事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.recommendbtn:
tabHost.setCurrentTabByTag("tab_test1");
break;
case R.id.strollbtn:
tabHost.setCurrentTabByTag("tab_test2");
break;
}
}
} );
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost android:id="@android:id/tabhost" //id android 默认
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--注意 设置此LinearLayout时,选项卡在底部,如去提,选项卡在屏幕顶部-->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent" //id android 默认
android:layout_width="fill_parent" android:layout_height="0.0dip"
android:layout_weight="1.0"/>
<!--注意切记 android:visibility="gone" 时,隐藏系统提供的选项卡,方便设计自定义选项卡-->
<TabWidget android:id="@android:id/tabs" //android 默认
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:visibility="gone" android:layout_weight="0.0"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ff919191"
android:paddingTop="1px">
<!-- 自定义radio组件-->
<RadioGroup android:gravity="center_vertical"
android:layout_gravity="bottom" android:orientation="horizontal"
android:id="@+id/main_radio" android:background="@color/skin_green"
android:layout_width="fill_parent" android:layout_height="50dp">
<RadioButton android:id="@+id/recommendbtn"
android:tag="recommendbtn" style="@style/style_recommendbtn" />
<RadioButton android:id="@+id/strollbtn" android:tag="strollbtn"
style="@style/style_strollbtn" />
<RadioButton android:id="@+id/specialbtn" android:tag="specialbtn"
</RadioGroup>
</LinearLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
TaboneActivity
public class TaboneActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
}
}
layout_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear1"
android:orientation="vertical">
<TextView android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一个页面"></TextView>
<LinearLayout android:id="@+id/content"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1.0" android:background="@color/background">
</LinearLayout>
</LinearLayout>
上一篇: TabHost+Fragment分析
下一篇: 太监是怎么诞生的?中国的第一个太监是谁?