新建项目之TabHost

很多朋友会遇见这么一个问题,项目写完了,需要新建一个项目,或者刚开始做项目,就会发现,整体框架怎么搭建,大多数都是在以前项目或者网上找个项目,在里面去复制代码,这样多累啊,因为一个成熟项目在框架里面会有很多东西是你新项目不需要的,对于下载别人代码而言看起来会比较麻烦。下面就来给大家写一个空的项目框架,主要就是Tabhost。



大体就长这个样子,下面四个按钮控制着四个Activity

先看布局吧:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="0.0dip"
                android:layout_weight="1.0"
                android:background="#ffffff" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1.5dp"
                android:background="#333333" />

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.0"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/home_radio_button_group"
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:layout_gravity="bottom"
                android:background="#333333"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/home_one_index"
                    style="@style/home_tab_bottom"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:checked="true"
                    android:drawableTop="@drawable/bottom_index_shape"
                    android:text="One"
                    android:textColor="#ffd600"
                    android:textSize="12sp" />


                <RadioButton
                    android:id="@+id/home_two_index"
                    style="@style/home_tab_bottom"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:drawableTop="@drawable/bottom_college_shape"
                    android:text="Two"
                    android:textColor="#878787"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/home_three_index"
                    style="@style/home_tab_bottom"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:drawableTop="@drawable/bottom_make_any_shape"
                    android:text="Three"
                    android:textColor="#878787"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/home_four_index"
                    style="@style/home_tab_bottom"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#00000000"
                    android:drawableTop="@drawable/bottom_my_shape"
                    android:text="Four"
                    android:textColor="#878787"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>

    </TabHost>


</RelativeLayout>

这里有三个东西:TabHost,TabWidget,FrameLayout  不要看着没用就去删掉,这个必须保留的,再仔细看看它们的id写法有些不一样。TabHost,TabWidget,FrameLayout三个标签对应的id必须是tabhost,tabs,tabcontent 。如果用TabHost的话, 上面标红的三处必须是一样, 这个是Google的约定。  而且一个工程中只能有一个TabHost。

如果不这样的话,举个简单的例子:

xml根元素选择TabHost, 但是ADT没有添加id属性, 运行的时候,会提示Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’错误, 因此需要添加android:id=”@android:id/tabhost”, 这样就可以了。

剩下就是怎么运用这个布局,当然在TabHost有自带的属性,记住,你的Activity是要继承TabActivity的,虽然已经过时

 mTabHost = getTabHost();
        Intent i_one = new Intent(this, OneActivity.class);
        Intent i_two = new Intent(this, TwoActivity.class);
        Intent i_three = new Intent(this, ThreeActivity.class);
        Intent i_four = new Intent(this, FourActivity.class);

        mTabHost.addTab(mTabHost.newTabSpec(TAB_ONE).setIndicator(TAB_ONE).setContent(i_one));
        mTabHost.addTab(mTabHost.newTabSpec(TAB_TWO).setIndicator(TAB_TWO).setContent(i_two));
        mTabHost.addTab(mTabHost.newTabSpec(TAB_THREE).setIndicator(TAB_THREE).setContent(i_three));
        mTabHost.addTab(mTabHost.newTabSpec(TAB_FOUR).setIndicator(TAB_FOUR).setContent(i_four));
然后就是在点击某一个RadioButton的时候去更改颜色和跳转到其它activity

initTextColor();
                home_two_index.setTextColor(Color.parseColor("#ffd600"));
                home_two_index.setChecked(true);
                mTabHost.setCurrentTabByTag(TAB_TWO);
    home_one_index.setTextColor(Color.parseColor("#878787"));
        home_two_index.setTextColor(Color.parseColor("#878787"));
        home_three_index.setTextColor(Color.parseColor("#878787"));
        home_four_index.setTextColor(Color.parseColor("#878787"));
        home_one_index.setChecked(false);
        home_two_index.setChecked(false);
        home_three_index.setChecked(false);
        home_four_index.setChecked(false);

就这么简单就搭建完成了,下面粘贴具体demo:

http://download.csdn.net/detail/greatdaocaoren/9912931

欢迎下载







猜你喜欢

转载自blog.csdn.net/greatdaocaoren/article/details/76223836