Tab效果之用TabHost而不用TabWidget用RadioGroup

前面已出了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这个监听方法里面


源码下载:

点击打开链接






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值