FragmentTabHost+Fragment实现底部菜单的切换

添加依赖

compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:design:24.0.0'

先看看运行结果

这里写图片描述

搭建主布局 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.cewidgets.MainActivity">

    <FrameLayout
        android:id="@+id/showTabFragmentContents"--》存放底部菜单切换时,触发的相应的Fragment
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>

    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black">

        <FrameLayout
            android:layout_width="0dp"--》不设置为零的话底部菜单栏不会显示
            android:layout_height="0dp"
            android:layout_weight="0"></FrameLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

搭建子布局 tabhost_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" --》全部充满屏幕
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dp">

    <ImageView
        android:id="@+id/tabHostItem_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_home_green_400_24dp" /> --》图片选择器

    <TextView
        android:id="@+id/tabHostItem_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@drawable/tab_iv_color_selector" -->字体颜色选择器
        android:layout_marginTop="1dp"
        android:text="首页"
        />
</LinearLayout>

在Activity中 MainActivity.java

public class MainActivity extends AppCompatActivity {


    private FragmentTabHost tabHost;
    private int[] tabHostItem_img = {R.drawable.tab_shouye_iv_seletor,
            R.drawable.tab_fenlei_iv_seletor,
            R.drawable.tab_mes_iv_seletor,
            R.drawable.tab_mine_iv_seletor};
    private String[] tabHostItem_tv = {"首页", "分类", "消息", "我的"};
    private Class[] tabHostFragments = {ShouYeFragment.class,
            FenLeiFragment.class,
            MsgFragment.class,
            MineFragment.class};


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

        initFragmentTabHost();
    }

    private void initFragmentTabHost() {
        tabHost= (FragmentTabHost) findViewById(R.id.tabHost);
        tabHost.setup(this,getSupportFragmentManager(),R.id.showTabFragmentContents);
        for (int i=0;i<tabHostItem_img.length;i++){
            TabHost.TabSpec tabSpec=tabHost.newTabSpec(tabHostItem_tv[i]).setIndicator(getTabHostView(i));
            tabHost.addTab(tabSpec,tabHostFragments[i],null);
        }
    }

    private View getTabHostView(int i) {
        View view= LayoutInflater.from(this).inflate(R.layout.tabhost_item,null);
        ImageView iv= (ImageView) view.findViewById(R.id.tabHostItem_iv);
        iv.setImageResource(tabHostItem_img[i]);
        TextView tv= (TextView) view.findViewById(R.id.tabHostItem_tv);
        tv.setText(tabHostItem_tv[i]);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值