Android中TabActivity的实现

public class MainActivity extends TabActivity {
	
	
	private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        
        tabHost=this.getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent=new Intent().setClass(this, StartActivity.class);
        spec=tabHost.newTabSpec("开始").setIndicator("开始").setContent(intent);
        tabHost.addTab(spec);
        
        intent=new Intent().setClass(this,PlanActivity.class);
        spec=tabHost.newTabSpec("计划").setIndicator("计划").setContent(intent);
        tabHost.addTab(spec);
        
        intent=new Intent().setClass(this, HistoryActivity.class);
        spec=tabHost.newTabSpec("历史").setIndicator("历史").setContent(intent);
        tabHost.addTab(spec);
        
     
        intent=new Intent().setClass(this, PersonalInformationActivity.class);
        spec=tabHost.newTabSpec("我").setIndicator("我").setContent(intent);
        tabHost.addTab(spec);
        
        // 默认设置选中start页面
        tabHost.setCurrentTab(0);
        
        RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.main_sport:
					tabHost.setCurrentTabByTag("开始");
					break;
				case R.id.main_plan:
					tabHost.setCurrentTabByTag("计划");
					break;
				case R.id.main_history:
					tabHost.setCurrentTabByTag("历史");
					break;
				case R.id.main_set:
					tabHost.setCurrentTabByTag("我");
					break;

				}
			}
		});
    }
    
    @Override
    protected void onStop(){
    	super.onStop();
    	
    }
    
    @Override
    protected void onDestroy(){
    	super.onDestroy();
    	
    }
    
}

layout文件如下:

<?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"
    android:background="#ffffff" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:background="#ffffff"
            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:background="#ffffff"
            android:visibility="gone" />

        <FrameLayout
            android:layout_width="fill_parent"
            android:background="#ffffff"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/main_tab_group"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:gravity="bottom"
                android:orientation="horizontal"
                android:background="#ffffff"
                 >

                <RadioButton
                    android:id="@+id/main_sport"
                    style="@style/MMTabButton"
                    android:layout_weight="1.0" 
                    android:checked="true"   
                    android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
                />

                <RadioButton
                    android:id="@+id/main_plan"
                    style="@style/MMTabButton"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
                 />

                <RadioButton
                    android:id="@+id/main_history"
                    style="@style/MMTabButton"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
                     />

                <RadioButton
                    android:id="@+id/main_set"
                    style="@style/MMTabButton"
                    android:layout_weight="1.0"   
                    android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
                  	/>
            </RadioGroup>

            
        </FrameLayout>
    </LinearLayout>

</TabHost>
什么是TabHost?

Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page[1].

什么是TabSpec?

A tab has a tab indicator, content, and a tag that is used to keep track of it. This builder helps choose among these options. For the tab indicator, your choices are: 1) set a label 2) set a label and an icon For the tab content, your choices are: 1) the id of a View 2) a TabHost.TabContentFactory that creates the View content. 3) an Intent that launches an Activity[2].

注意For apps developing against HONEYCOMB or later, tabs are typically presented in the UI using the newActionBar.newTab() and related APIs for placing tabs within their action bar area[2].


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值