Android学习――TabHost的使用二

仿QQ底端导航栏

wKiom1T6sPPS1OTnAAEoTExVHIA053.jpg

wKioL1T6sgygX9dhAAEylMXKhxk160.jpg

wKiom1T6sPPBShZ_AAEE8wtu46o405.jpg

废话不多说,直接上代码

<?xml version="1.0" encoding="UTF-8"?>
<!-- Tab导航 最新版 -->
<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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#FFF" >

        </FrameLayout>

        <!-- TabWidget管理所有的选项卡,id名是android指定的 -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone" />

        <!-- Frame下放置单选群组替代TAB效果 -->
        <RadioGroup
            android:id="@+id/main_radio"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#ccc"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true" >

            <RadioButton
                android:id="@+id/tab_icon_qq"
                style="@style/main_tab"
                android:checked="true"
                android:drawableTop="@drawable/message"
                android:text="消息" />

            <RadioButton
                android:id="@+id/tab_icon_address"
                style="@style/main_tab_bottom"
                android:checked="false"
                android:drawableTop="@drawable/home"
                android:text="班级" />

                 <RadioButton
                android:id="@+id/tab_icon_myself"
                style="@style/main_tab_bottom"
                android:drawableTop="@drawable/d1"
                android:checked="false"
                android:text="我" />
        </RadioGroup>
    </RelativeLayout>
</TabHost>

MainActivity

public class MainActivity  extends TabActivity {

    public static TabHost mTabHost;
    public static TabHost getmTabHost() {
        return mTabHost;
    }

    private RadioGroup main_radio;
    private RadioButton tab_icon_qq, tab_icon_address,tab_icon_myself;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabHost = getTabHost();
        final TabWidget tabWidget = mTabHost.getTabWidget();
        tabWidget.setStripEnabled(false);
            mTabHost.addTab(mTabHost.newTabSpec("TAG1").setIndicator("0").setContent(new Intent(this,MessageActivity.class)));
        mTabHost.addTab(mTabHost.newTabSpec("TAG2").setIndicator("1").setContent(new Intent(this,ClassActivity.class)));
          mTabHost.addTab(mTabHost.newTabSpec("TAG3").setIndicator("2").setContent(new Intent(this,MyselfActivity.class)));
        main_radio = (RadioGroup) findViewById(R.id.main_radio);
        tab_icon_weixin = (RadioButton) findViewById(R.id.tab_icon_weixin);
        tab_icon_address = (RadioButton) findViewById(R.id.tab_icon_address);

        tab_icon_myself = (RadioButton) findViewById(R.id.tab_icon_myself);
        main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int id) {
                if (id == tab_icon_weixin.getId()) {
                    mTabHost.setCurrentTab(0);
                } else if (id == tab_icon_address.getId()) {
                    mTabHost.setCurrentTab(1);

                } else if (id == tab_icon_myself.getId()) {
                    mTabHost.setCurrentTab(2);
                }
            }
        });

        // 设置当前显示哪一个标签
        mTabHost.setCurrentTab(0);
        // 遍历tabWidget每个标签,
        for (int i = 0; i < tabWidget.getChildCount(); i++) {
            View vv = tabWidget.getChildAt(i);
            vv.getLayoutParams().height = 45;
            // vv.getLayoutParams().width = 65;
            vv.setBackgroundColor(Color.BLUE);
        }
//      findViewById(R.id.tab_icon_brand).setOnClickListener(this);
    }
}


你可能感兴趣的:(android,tabhost)