美文网首页
底部菜单栏FragmentTabHost+Fragment

底部菜单栏FragmentTabHost+Fragment

作者: 熊大哥87 | 来源:发表于2017-08-01 09:55 被阅读0次

    首先是底部菜单栏的布局

    <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=".MainActivity">
    
    
        <FrameLayout//真正的菜单内容
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:background="@color/bg_color"
            />
    
        <android.support.v4.app.FragmentTabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            >
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0"/>
    
        </android.support.v4.app.FragmentTabHost>
    
    
    </LinearLayout>
    
            Tab tab_home=new Tab(R.string.home,R.drawable.selector_icon_home,HomeFragment.class);
            Tab tab_hot=new Tab(R.string.hot,R.drawable.selector_icon_hot,HotFragment.class);
            Tab tab_category=new Tab(R.string.catagory,R.drawable.selector_icon_category, CategoryFragment.class);
            Tab tab_cart=new Tab(R.string.cart,R.drawable.selector_icon_cart,CartFragment.class);
            Tab tab_mine=new Tab(R.string.mine,R.drawable.selector_icon_mine,MineFragment.class);
            mTabs.add(tab_home);
            mTabs.add(tab_hot);
            mTabs.add(tab_category);
            mTabs.add(tab_cart);
            mTabs.add(tab_mine);
            mInflater=LayoutInflater.from(this);
            mTabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
            for (Tab tab:mTabs){
                TabHost.TabSpec tabSpec=mTabHost.newTabSpec(getString(tab.getTitle()));
                View  view=buildindicator(tab);
                tabSpec.setIndicator(view);
                mTabHost.addTab(tabSpec,tab.getFragment(),null);
            }
            mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
            mTabHost.setCurrentTab(0);
    
        }
        private View buildindicator(Tab tab){
            View view=mInflater.inflate(R.layout.tab_indicator,null);
            ImageView img= (ImageView) view.findViewById(R.id.icon_tab);
            TextView text= (TextView)view.findViewById(R.id.txt_indicator);
            img.setBackgroundResource(tab.getIcon());
            text.setText(tab.getTitle());
            return view;
        }
    

    相关文章

      网友评论

          本文标题:底部菜单栏FragmentTabHost+Fragment

          本文链接:https://www.haomeiwen.com/subject/pbqrlxtx.html