学习笔记 新浪微博篇 五 Android 利用RadioGroup自定义TableHost

是我们要用

mainTabSpec.setContent(new Intent(this,MainTabActivity.class));

因为setContent(括号里是一个intint)

所以要设置

tabHost.setup(LocalActivityManager的类);

为了得到LocalActivityManager类

就必须使用
this.getLocalActivityManager()方法

而这个方法必须要当前类继承ActivityGroup

这里是MyActivity类

public class MyActivity extends ActivityGroup {

    private static final String HOME_TAB = "home";
    private static final String AT_TAB = "at";
    private static final String MSG_TAB = "msg";
    private static final String SEARCH_TAB = "search";
    private static final String MENU_TAB = "menu";


    private TabHost tabHost;
    private RadioGroup radioGroup;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tabHost = (TabHost)findViewById(R.id.main_tab_host);

        //初始化tabHost 传递LocalActivityManager
        tabHost.setup(this.getLocalActivityManager());

        //穿件 TabSpec 
        TabHost.TabSpec mainTabSpec = tabHost.newTabSpec(HOME_TAB);
        TabHost.TabSpec atTabSpec = tabHost.newTabSpec(AT_TAB);
        TabHost.TabSpec msgTabSpec = tabHost.newTabSpec(MSG_TAB);
        TabHost.TabSpec searchTabSpec = tabHost.newTabSpec(SEARCH_TAB);
        TabHost.TabSpec moreTabSpec = tabHost.newTabSpec(MENU_TAB);

        //设置TabSpec 对应的activity
        mainTabSpec.setIndicator(HOME_TAB).setContent(new Intent(this,MainTabActivity.class));
        atTabSpec.setIndicator(AT_TAB).setContent(new Intent(this,AtTabActivity.class));
        msgTabSpec.setIndicator(MSG_TAB).setContent(new Intent(this,MenuTabActivity.class));
        searchTabSpec.setIndicator(SEARCH_TAB).setContent(new Intent(this,SearchTabActivity.class));
        moreTabSpec.setIndicator(MENU_TAB).setContent(new Intent(this,MenuTabActivity.class));


        //添加到mTabSpecs  List里
        tabHost.addTab(mainTabSpec);
        tabHost.addTab(atTabSpec);
        tabHost.addTab(msgTabSpec);
        tabHost.addTab(searchTabSpec);
        tabHost.addTab(moreTabSpec);

        //获取radioGroup
        radioGroup = (RadioGroup) findViewById(R.id.rg_main_btns);

        //radioGroup监听事件
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    //通过tag的名字来设置当前面
                    case R.id.rd_btn_main:
                        tabHost.setCurrentTabByTag(HOME_TAB);
                        break;

                    case R.id.rd_btn_at:
                        tabHost.setCurrentTabByTag(AT_TAB);
                        break;

                    case R.id.rd_btn_message:
                        tabHost.setCurrentTabByTag(MSG_TAB);
                        break;

                    case R.id.rd_btn_search:
                        tabHost.setCurrentTabByTag(SEARCH_TAB);
                        break;

                    case R.id.rd_btn_menu:
                        tabHost.setCurrentTabByTag(MENU_TAB);
                        break;

                    default :
                        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="@+id/main_tab_host"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">



        <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone">
        </TabWidget>


        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <RadioGroup
                    android:id="@+id/rg_main_btns"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:orientation="horizontal"
                    android:background="@color/ivory">
                <RadioButton
                        android:id="@+id/rd_btn_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/main_tab"
                        style="@style/main_btn_style"
                        android:drawableTop="@drawable/main_page_btn"
                        />

                <RadioButton
                        android:id="@+id/rd_btn_at"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/at_tab"
                        style="@style/main_btn_style"
                        android:drawableTop="@drawable/at_gage_btn"
                />

                <RadioButton
                        android:id="@+id/rd_btn_message"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/msg_tab"
                        style="@style/main_btn_style"
                        android:drawableTop="@drawable/message_page_btn"
                />

                <RadioButton
                        android:id="@+id/rd_btn_search"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/search_tab"
                        style="@style/main_btn_style"
                        android:drawableTop="@drawable/search_page_btn"
                />

                <RadioButton
                        android:id="@+id/rd_btn_menu"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/menu_tab"
                        style="@style/main_btn_style"
                        android:drawableTop="@drawable/menu_page_btn"
                />
            </RadioGroup>
        </FrameLayout>

    </TabHost>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值