TabHost切换view

1.布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#043"
     >

 <RadioGroup
        android:id="@+id/mainscreen_radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/btn_normal"
        android:orientation="horizontal" >

        

        <RadioButton
            android:id="@+id/one"
            style="@style/mainscreen_tab_btn"
            android:background="@drawable/selector_tab_btn"
             android:checked="true"
            android:text="one" />

        <RadioButton
            android:id="@+id/two"
            style="@style/mainscreen_tab_btn"
            android:background="@drawable/selector_tab_btn"
            android:text="two" />
        
        <RadioButton
            android:id="@+id/three"
            style="@style/mainscreen_tab_btn"
            android:background="@drawable/selector_tab_btn"
           
            android:text="three" />
    </RadioGroup>
    <TabHost
        android:id="@+id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:layout_below="@id/mainscreen_radiogroup"
        android:layout_weight="1.0" >

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/white" >

            <LinearLayout
                android:id="@+id/layout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="one" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/layout2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="two" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/layout3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="three" />
            </LinearLayout>
        </FrameLayout>
    </TabHost>

</RelativeLayout>



package cn.bo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

public class DemoTabhostActivity extends Activity {
    
	private TabHost tabHost;
	public static RadioGroup radioGroup;
	public static final String TAB_OPTIMIZATION = "taboptimization";
	public static final String TAB_SAVE = "tabsave";
	public static final String TAB_MONITOR = "tabmonitor";
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demotabhost);
        tabHost=(TabHost)findViewById(R.id.tabs);
        tabHost.setup();//必须调用实例化控件、
        
        tabHost.addTab(tabHost.newTabSpec(TAB_OPTIMIZATION)
                .setIndicator("tab1")
                .setContent(R.id.layout1));
        tabHost.addTab(tabHost.newTabSpec(TAB_SAVE)
                .setIndicator("tab2")
                .setContent(R.id.layout2));
        tabHost.addTab(tabHost.newTabSpec(TAB_MONITOR)
                .setIndicator("tab3")
                .setContent(R.id.layout3));
        
        
        radioGroup=(RadioGroup)this.findViewById(R.id.mainscreen_radiogroup);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    		
    		@Override
    		public void onCheckedChanged(RadioGroup group, int checkedId) {
    			switch (checkedId) {
    			case R.id.one:
    				
    				tabHost.setCurrentTabByTag(TAB_OPTIMIZATION);
    				break;
    			case R.id.two:
    				
    				tabHost.setCurrentTabByTag(TAB_SAVE);
    				break;
    			case R.id.three:
    				
    				tabHost.setCurrentTabByTag(TAB_MONITOR);	
    				break;
    			}
    			
    		}
        
     });
        
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值