您的位置:首页 > 移动开发 > Android开发

通过TabActivity创建底部菜单栏

2014-08-14 17:02 344 查看
隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏,因为这种方法更灵活,界面更容易控制,更好看。大部分应用程序基本上都是使用这种方法来实现的。通过setCurrentTabByTag()方法来切换不同的选项卡。

首先创建一个布局界面:

[html] view
plaincopy

<?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="0.0dip"  

            android:layout_weight="1.0" />  

  

        <TabWidget  

            android:id="@android:id/tabs"  

            android:layout_width="fill_parent"  

            android:layout_height="wrap_content"  

            android:layout_weight="0.0"  

            android:visibility="gone" />  

  

        <RadioGroup  

            android:id="@+id/main_radiogroup"  

            android:layout_width="fill_parent"  

            android:layout_height="wrap_content"  

            android:layout_gravity="bottom"  

            android:background="@drawable/tab_widget_background"  

            android:gravity="center_vertical"  

            android:orientation="horizontal"  

            android:padding="2dip" >  

  

            <RadioButton  

                android:id="@+id/RadioButton0"  

                style="@style/tab_item_background"  

                android:drawableTop="@drawable/tab_icon1"  

                android:text="主页" />  

  

            <RadioButton  

                android:id="@+id/RadioButton1"  

                style="@style/tab_item_background"  

                android:drawableTop="@drawable/tab_icon2"  

                android:text="关于" />  

  

            <RadioButton  

                android:id="@+id/RadioButton2"  

                style="@style/tab_item_background"  

                android:drawableTop="@drawable/tab_icon3"  

                android:text="设置" />  

  

            <RadioButton  

                android:id="@+id/RadioButton3"  

                style="@style/tab_item_background"  

                android:drawableTop="@drawable/tab_icon4"  

                android:text="搜索" />  

  

            <RadioButton  

                android:id="@+id/RadioButton4"  

                style="@style/tab_item_background"  

                android:drawableTop="@drawable/tab_icon5"  

                android:text="更多" />  

        </RadioGroup>  

    </LinearLayout>  

  

</TabHost> 

2、然后在定义几个用来存放Tab选项卡内容的activity布局文件,这里只列出一个activity1_layout.xml:

[html] view
plaincopy

<?xml version="1.0" encoding="utf-8"?>  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent" >  

  

    <ImageView  

        android:id="@+id/imageview"  

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent"  

        android:scaleType="fitCenter"  

        android:src="@drawable/xianjian01" >  

    </ImageView>  

  

</LinearLayout>  

3、最后再定义一个自定义Tab按钮的资源文件,selector_tab_background2.xml:

[html] view
plaincopy

<?xml version="1.0" encoding="utf-8"?>  

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

  

    <item android:drawable="@drawable/tab_item_p" android:state_pressed="true"/>  

    <item android:drawable="@drawable/tab_item_d" android:state_checked="true"/>  

  

</selector>  

4、布局界面讲解完毕,接下来详细讲解java代码

[java] view
plaincopy

package com.yangyu.mycustomtab01;  

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;  

  

import com.yangyu.mycustomtab01.Constant.ConValue;  

  

/** 

 * @author yangyu 

 *  功能描述:第二种实现方式,自定义RadioGroup 

 */  

public class CustomTabActivity2 extends TabActivity{  

      

    //定义TabHost对象  

    private TabHost tabHost;  

      

    //定义RadioGroup对象  

    private RadioGroup radioGroup;  

      

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main_tab_layout2);  

          

        initView();  

          

        initData();  

    }  

      

    /** 

     * 初始化组件 

     */  

    private void initView(){  

        //实例化TabHost,得到TabHost对象  

        tabHost = getTabHost();  

          

        //得到Activity的个数  

        int count = ConValue.mTabClassArray.length;               

                  

        for(int i = 0; i < count; i++){    

            //为每一个Tab按钮设置图标、文字和内容  

            TabSpec tabSpec = tabHost.newTabSpec(ConValue.mTextviewArray[i]).setIndicator(ConValue.mTextviewArray[i]).setContent(getTabItemIntent(i));  

            //将Tab按钮添加进Tab选项卡中  

            tabHost.addTab(tabSpec);  

        }  

          

        //实例化RadioGroup  

        radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);  

    }  

      

    /** 

     * 初始化组件 

     */  

    private void initData() {  

        // 给radioGroup设置监听事件  

        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  

            @Override  

            public void onCheckedChanged(RadioGroup group, int checkedId) {  

                switch (checkedId) {  

                case R.id.RadioButton0:  

                    tabHost.setCurrentTabByTag(ConValue.mTextviewArray[0]);  

                    break;  

                case R.id.RadioButton1:  

                    tabHost.setCurrentTabByTag(ConValue.mTextviewArray[1]);  

                    break;  

                case R.id.RadioButton2:  

                    tabHost.setCurrentTabByTag(ConValue.mTextviewArray[2]);  

                    break;  

                case R.id.RadioButton3:  

                    tabHost.setCurrentTabByTag(ConValue.mTextviewArray[3]);  

                    break;  

                case R.id.RadioButton4:  

                    tabHost.setCurrentTabByTag(ConValue.mTextviewArray[4]);  

                    break;  

                }  

            }  

        });  

        ((RadioButton) radioGroup.getChildAt(0)).toggle();  

    }  

      

    /** 

     * 给Tab选项卡设置内容(每个内容都是一个Activity) 

     */  

    private Intent getTabItemIntent(int index){  

        Intent intent = new Intent(this, ConValue.mTabClassArray[index]);     

        return intent;  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐