在Android中我们经常用到顶部或底部选项卡导航,下面对该功能进行一下实现
首先在layout文件夹中添加一个main_tabs文件,代码如下:
<?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="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0">
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.0">
</TabWidget>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:background="@color/commit_company_color">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:button="@null"
android:singleLine="true"
android:gravity="center_horizontal"
android:id="@+id/radio_button0"
android:layout_marginTop="2.0dip"
android:text="成长"
android:background="@null"
android:drawableTop="@drawable/bg_main_page_button"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:singleLine="true"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:button="@null"
android:id="@+id/radio_button1"
android:layout_marginTop="2.0dip"
android:text="首页"
android:background="@null"
android:drawableTop="@drawable/bg_main_page_button"/>
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center_horizontal"
android:layout_gravity="center"
android:button="@null"
android:singleLine="true"
android:id="@+id/radio_button2"
android:layout_marginTop="2.0dip"
android:background="@null"
android:text="我的"
android:drawableTop="@drawable/bg_main_page_button"/>
</RadioGroup>
</LinearLayout>
</TabHost>
其中TabHost 、FrameLayout、TabWidget的id为Android定义好的写法,如果写错会报找不到资源的错误。 TabWidget是tab的组件,FrameLayout是每一个选项卡内容的容器。
然后是mainActivity中的代码:
public class MyActivity extends TabActivity implements CompoundButton.OnCheckedChangeListener {
private TabHost mTabHost;
private Intent mAIntent;
private Intent mBIntent;
private Intent mCIntent;
private RadioButton mButtonOne;
private RadioButton mButtonTwo;
private RadioButton mButtonThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_tabs);
mTabHost = getTabHost();
mMainIntent = new Intent(this , Activity_1.class);
mGroupRecordIntent = new Intent(this,
Activity_2.class);
mUserCenterIntent = new Intent(this ,
Activity_3.class);
mButtonOne = (RadioButton)findViewById(R.id.radio_button0);
mButtonTwo = (RadioButton)findViewById(R.id.radio_button1);
mButtonThree = (RadioButton)findViewById(R.id.radio_button2);
mButtonOne.setOnCheckedChangeListener(this);
mButtonTwo.setOnCheckedChangeListener(this);
mButtonThree.setOnCheckedChangeListener(this);
setupIntent();
mTabHost.setCurrentTabByTag("B_TAB");
mButtonTwo.setChecked(true);
}
private void setupIntent(){
mTabHost.addTab(buildTabSpec("A_TAB" , R.string.group_record , R.drawable.tag_brand_group_nor , mAIntent));
mTabHost.addTab(buildTabSpec("B_TAB" , R.string.main_page , R.drawable.
tag_brand_group_nor , mBIntent));
mTabHost.addTab(buildTabSpec("C_TAB" , R.string.my_page , R.drawable.tag_brand_group_nor , mCIntent));
}
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
final Intent content) {
return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
getResources().getDrawable(resIcon)).setContent(content);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switch (buttonView.getId()){
case R.id.radio_button0:
mTabHost.setCurrentTabByTag("A_TAB");
break;
case R.id.radio_button1:
mTabHost.setCurrentTabByTag("B_TAB");
break;
case R.id.radio_button2:
mTabHost.setCurrentTabByTag("C_TAB");
break;
}
}
}
}
mTabHost.setCurrentTabByTag("B_TAB");
mButtonTwo.setChecked(true); 负责初始显示中间的tab,并且设置中间tab的radiobutton为选中状态。