TabHost嵌套Activity实现底部菜单栏切换

 效果如下:(图标是自己下的,文章里会给出来)

 先来看下目录结构:

TabMainActivity最外层的activity

 这是activity对应的布局文件(下面还有其他的layout,这里就不截图了,会给出代码的)

 下面放上底部选项卡的图片(选中与未选中各一张)

weixin_normal.png

 weixin_selected.png

 

 tongxunlu_normal.png

 

 tongxunlu_selected.png

 

 faxian_normal.png

 

 faxian_selected.png

 

 mine_normal.png

 

 mine_selected.png

还有activity_tab_main.xml中TabWidget外层RelativeLayout的背景图片tab_bom_bg.png(不知道为什么放到这里显示不出来,自己替换成别的图片吧)

Tab选项卡图片的背景布局,引入了选中和未选中状态下图片改变的selector

 weixin_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="25dp"
    android:id="@+id/home_tab_img_icon"
    android:src="@drawable/weixin_img_selector"
    android:adjustViewBounds="true"
    android:contentDescription="@null"
    />

tongxunlu_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:id="@+id/home_tab_img_icon"
    android:src="@drawable/tongxunlu_img_selector"
    android:adjustViewBounds="true"
    android:contentDescription="@null"
    />

faxian_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingLeft="25dp"
    android:paddingRight="25dp"
    android:id="@+id/home_tab_img_icon"
    android:src="@drawable/faxian_img_selector"
    android:adjustViewBounds="true"
    android:contentDescription="@null"
    />

wode_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    android:paddingLeft="25dp"
    android:paddingRight="20dp"
    android:id="@+id/home_tab_img_icon"
    android:src="@drawable/mine_img_selector"
    android:adjustViewBounds="true"
    android:contentDescription="@null"
    />

 选中和未选中状态下图片改变的selector

 weixin_img_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--获得焦点,单击时候的图片-->
    <item android:state_pressed="true" android:drawable="@drawable/weixin_selected"/>

    <!--选中时的图片-->
    <item android:state_selected="true" android:drawable="@drawable/weixin_selected"/>

    <!--默认,没选中的图片-->
    <item android:state_selected="false" android:drawable="@drawable/weixin_normal"/>
</selector>

tongxunlu_img_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--获得焦点,单击时候的图片-->
    <item android:state_pressed="true" android:drawable="@drawable/tongxunlu_selected"/>

    <!--选中时的图片-->
    <item android:state_selected="true" android:drawable="@drawable/tongxunlu_selected"/>

    <!--默认,没选中的图片-->
    <item android:state_selected="false" android:drawable="@drawable/tongxunlu_normal"/>
</selector>

faxian_img_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--获得焦点,单击时候的图片-->
    <item android:state_pressed="true" android:drawable="@drawable/faxian_selected"/>

    <!--选中时的图片-->
    <item android:state_selected="true" android:drawable="@drawable/faxian_selected"/>

    <!--默认,没选中的图片-->
    <item android:state_selected="false" android:drawable="@drawable/faxian_normal"/>
</selector>

mine_img_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--获得焦点,单击时候的图片-->
    <item android:state_pressed="true" android:drawable="@drawable/mine_selected"/>

    <!--选中时的图片-->
    <item android:state_selected="true" android:drawable="@drawable/mine_selected"/>

    <!--默认,没选中的图片-->
    <item android:state_selected="false" android:drawable="@drawable/mine_normal"/>
</selector>

接下来是activity对应的布局页面

activity_tab_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.littlejie.app.tabhost.TabMainActivity">

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

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

        <RelativeLayout
            android:background="@drawable/tab_bom_bg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TabWidget
            android:gravity="center_horizontal"
            android:layout_centerHorizontal="true"
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="50dp"/>

        </RelativeLayout>
    </LinearLayout>
</TabHost>

activity_tab_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.littlejie.app.tabhost.TabOneActivity">

    <RelativeLayout
        android:background="@color/color_title_black"
        android:layout_width="match_parent"
        android:layout_height="38dp">

        <TextView
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:text="微信"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:textSize="17sp"
        android:text="TabOneActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

activity_tab_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.littlejie.app.tabhost.TabOneActivity">

    <RelativeLayout
        android:background="@color/color_title_black"
        android:layout_width="match_parent"
        android:layout_height="38dp">

        <TextView
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:text="通讯录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:textSize="17sp"
            android:text="TabTwoActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

activity_tab_three.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.littlejie.app.tabhost.TabOneActivity">

    <RelativeLayout
        android:background="@color/color_title_black"
        android:layout_width="match_parent"
        android:layout_height="38dp">

        <TextView
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:text="发现"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:textSize="17sp"
            android:text="TabThreeActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

activity_tab_four.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.littlejie.app.tabhost.TabOneActivity">

    <RelativeLayout
        android:background="@color/color_title_black"
        android:layout_width="match_parent"
        android:layout_height="38dp">

        <TextView
            android:textSize="18sp"
            android:layout_centerInParent="true"
            android:textColor="#ffffff"
            android:text="我"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <LinearLayout
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:textSize="17sp"
            android:text="TabFourActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

TabHost常量工具类Constant

public class Constant {

    public static final class ConValue{

        /*
        * tab选项卡的图标布局,带有选中和未选中的图片切换
        * */
        public static int mLayoutViewArray[] = {
                R.layout.weixin_tab_layout,
                R.layout.tongxunlu_tab_layout,
                R.layout.faxian_tab_layout,
                R.layout.wode_tab_layout
        };
        /*
        * tab选项卡的文字
        * */
        public static String mTextViewArray[] ={
        "微信","通讯录","发现","我"
        };

        /*
        * 每个tab显示的activity页面
        * */
        public static Class<?> mTabClassArray[] = {
                TabOneActivity.class,
                TabTwoActivity.class,
                TabThreeActivity.class,
                TabFourActivity.class
        };
    }
}

TabMainActivity

public class TabMainActivity extends TabActivity {
    
    private TabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_main);

        initView();
    }


    /*
    * 初始化组件
    * */
    private void initView() {
        //实例化TabHost对象
        mTabHost = getTabHost();

        //得到activity的个数
        int count = Constant.ConValue.mTabClassArray.length;

        for (int i = 0;i < count; i++){
            //为每一个tab按钮设置图标,文字和内容
            TabSpec tabSpec = mTabHost.
                    newTabSpec(Constant.ConValue.mTextViewArray[i])//文字
                    .setIndicator(getTabItemViewNew(i))//图标
                    .setContent(getTabItemIntent(i));//内容。也就是存放的activity

            //将tab按钮添加进tab选项卡中
            mTabHost.addTab(tabSpec);
            //设置tab按钮的背景
        }

    }

    /*
    * 给tab设置每个选项的内容,每个内容就是一个actiity
    * */
    private Intent getTabItemIntent(int index) {
        Intent intent = new Intent(this, Constant.ConValue.mTabClassArray[index]);
        return intent;
    }


    /*
   * 给tab按钮设置图标和文字
   * */
    private View getTabItemViewNew(int index){
        View view = LayoutInflater.from(this).inflate(Constant.ConValue.mLayoutViewArray[index],null);

        return view;
    }
}

 TabOneActivity,TabTwoActivity,TabThreeActivity,TabFourActivity里面没有做操作

此处就放上TabOneActivity

public class TabOneActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_one);

    }
}

此处我简单实现了一下沉浸式,就是修改了一下styles.xml中的几个颜色,这个颜色需要在colors.xml里定义

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/color_title_black</item>
        <item name="colorPrimaryDark">@color/color_title_black</item>
        <item name="colorAccent">@color/color_title_black</item>
    </style>

 这个颜色可以随意定义,我这里定义的是这个颜色

<color name = "color_title_black">#2b2a2a</color>
发布了110 篇原创文章 · 获赞 32 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览