前面已出了TabHost+TabWidget,RadioGroup+Fragment,上次我的大学组员问我,TabHost+RadioGroup怎么实现,但是自己没怎么弄过,今天把这个效果整理出来了呢!本人是菜鸟级别的,有不对的请指出!
效果图如下:
项目的架构如下:
1、主布局的xml,如下:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
></FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="gone"
></TabWidget>
<View
android:id="@+id/div_view"
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_above="@+id/main_radiogroup"
android:layout_marginBottom="2dip"
android:background="#ffc9cacb" />
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dip"
android:paddingBottom="10dip"
>
<RadioButton
android:id="@+id/first"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@null"
android:text="第一个选项"
android:padding="5dip"
android:gravity="center"
style="@style/textview_bg"
/>
<RadioButton
android:id="@+id/two"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@null"
android:text="第二个选项"
android:padding="5dip"
android:gravity="center"
style="@style/textview_bg"
/>
<RadioButton
android:id="@+id/three"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@null"
android:text="第三个选项"
android:padding="5dip"
android:gravity="center"
style="@style/textview_bg"
/>
</RadioGroup>
</LinearLayout>
</TabHost>
2、Java代码如下:
package com.tab_bamboo_tabhost.radiogroup;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
/** tabHost控件 */
private TabHost mTabHost;
/** 底部导航的布局 */
private RadioGroup main_radiogroup;
/** 选项卡的内容activity */
private Class[] activitys = { FirstActivity.class, SecondActivity.class,
ThreeActivity.class };
/** 这个只是选项卡的Tag*/
private String[] mTextName = { "选项一", "选项二", "选项三" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** 初始化控件 */
initView();
/** 初始化数据 */
initData();
}
/***
* 初始化控件
*/
private void initView() {
/** 因为我们是继承了TabActivity,所以才有以下方法,而且不需要手动的调用setup() */
mTabHost = getTabHost();
for (int i = 0; i < activitys.length; i++) {
/**
* 因为我们是用radiogroup,所以这里的setIndicator里面设置什么内容都是没有效果的,但是这个方法不执行的话会报错的
*/
TabSpec ts = mTabHost.newTabSpec(mTextName[i]).setIndicator("")
.setContent(getTabItemIntent(i));
mTabHost.addTab(ts);
}
main_radiogroup = (RadioGroup) this.findViewById(R.id.main_radiogroup);
}
private void initData() {
/** 选项卡之前的切换 */
main_radiogroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first:
mTabHost.setCurrentTabByTag(mTextName[0]);
break;
case R.id.two:
mTabHost.setCurrentTabByTag(mTextName[1]);
break;
case R.id.three:
mTabHost.setCurrentTabByTag(mTextName[2]);
break;
}
}
});
/** 之前进入默认选择第一项的时候都是采用切换方法的,一下这个方法执行的话,会进入onCheckedChanged 这个方法里面 */
((RadioButton) main_radiogroup.getChildAt(0)).toggle();
}
/***
* 给Tab选项卡设置内容(每个内容都是一个Activity)
*/
private Intent getTabItemIntent(int index) {
Intent intent = new Intent(MainActivity.this, activitys[index]);
return intent;
}
}
这里简单的介绍下,注释里面也说了虽然我们setIndicator()这个方法,我们传入的参数无效,但是我们把它去掉是会报错的原因是为什么呢?我贴出里面的源码你们就明白了
上面RadioButton 的toggle方法API上面的解释是:
通过有道的翻译:
改变的检查状态视图的逆其当前状态
如果单选按钮已经选中,这种方法不会切换单选按钮。
我刚刚也通过了debug调试了下,如果我把radiobutton的android:checked="true" 这个toggle方法执行后,不会进入到setOnCheckedChangeListener这个监听方法里面
源码下载: