TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity

 

 效果图:

TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity

首先看布局文件:

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

<TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"

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

    <LinearLayout 

        android:orientation="vertical" 

        android:layout_width="fill_parent" 

        android:layout_height="fill_parent">

        <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:visibility="gone" 

            android:layout_width="fill_parent" 

            android:layout_height="wrap_content" 

            android:layout_weight="0.0" />

        <RadioGroup 

            android:gravity="center_vertical" 

            android:layout_gravity="bottom" 

            android:orientation="horizontal" 

            android:id="@+id/main_radio" 

            android:background="@drawable/maintab_toolbar_bg" 

            android:layout_width="fill_parent" 

            android:layout_height="wrap_content">

            <RadioButton 

                android:id="@+id/radio_button0" 

                android:tag="radio_button0" 

                android:layout_marginTop="2.0dip" 

                android:text="@string/alarm" 

                android:drawableTop="@drawable/icon_1" 

                style="@style/main_tab_bottom" />

            <RadioButton 

                android:id="@+id/radio_button1" 

                android:tag="radio_button1" 

                android:layout_marginTop="2.0dip" 

                android:text="@string/message" 

                android:drawableTop="@drawable/icon_2" 

                style="@style/main_tab_bottom" />

            <RadioButton 

                android:id="@+id/radio_button2" 

                android:tag="radio_button2" 

                android:layout_marginTop="2.0dip" 

                android:text="@string/photo" 

                android:drawableTop="@drawable/icon_3" 

                style="@style/main_tab_bottom" />

            <RadioButton 

                android:id="@+id/radio_button3" 

                android:tag="radio_button3" 

                android:layout_marginTop="2.0dip" 

                android:text="@string/music" 

                android:drawableTop="@drawable/icon_4" 

                style="@style/main_tab_bottom" />

            <RadioButton 

                android:id="@+id/radio_button4" 

                android:tag="radio_button4" 

                android:layout_marginTop="2.0dip" 

                android:text="@string/setting" 

                android:drawableTop="@drawable/icon_5" 

                style="@style/main_tab_bottom" />

        </RadioGroup>

    </LinearLayout>

</TabHost>

 

 

需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id="@android:id/tabhost   ;android:id="@android:id/tabcontent" ;android:id="@android:id/tabs" ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件 
在TabActivity中有么个方法:

 

 

 1 package com.iteye.androidtoast;

 2 

 3 import android.app.TabActivity;

 4 import android.content.Intent;

 5 import android.os.Bundle;

 6 import android.widget.RadioGroup;

 7 import android.widget.RadioGroup.OnCheckedChangeListener;

 8 import android.widget.TabHost;

 9 

10 public class MainActivity extends TabActivity implements OnCheckedChangeListener{

11     

12     private TabHost mHost;

13     private RadioGroup radioderGroup;

14     

15     public void onCreate(Bundle savedInstanceState) {

16         super.onCreate(savedInstanceState);

17         setContentView(R.layout.maintabs);

18         //实例化TabHost

19         mHost=this.getTabHost();

20         

21       //添加选项卡

22         mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")

23                     .setContent(new Intent(this,OneActivity.class)));

24         mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")

25                 .setContent(new Intent(this,TwoActivity.class)));

26         mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")

27                 .setContent(new Intent(this,ThreeActivity.class)));

28         mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")

29                 .setContent(new Intent(this,FourActivity.class)));

30         mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")

31                 .setContent(new Intent(this,FiveActivity.class)));

32         

33         radioderGroup = (RadioGroup) findViewById(R.id.main_radio);

34         radioderGroup.setOnCheckedChangeListener(this);

35     }

36      

37     public void onCheckedChanged(RadioGroup group, int checkedId) {

38         switch(checkedId){

39         case R.id.radio_button0:

40             mHost.setCurrentTabByTag("ONE");

41             break;

42         case R.id.radio_button1:

43             mHost.setCurrentTabByTag("TWO");

44             break;

45         case R.id.radio_button2:

46             mHost.setCurrentTabByTag("THREE");

47             break;

48         case R.id.radio_button3:

49             mHost.setCurrentTabByTag("FOUR");

50             break;

51         case R.id.radio_button4:

52             mHost.setCurrentTabByTag("FIVE");

53             break;

54         }        

55     }

56 }

 

源码下载

点击下载

 

你可能感兴趣的:(Activity)