DrawerLayout+FragmentTabHost+ToolBar实现左滑菜单及fragment切换

1、drawerlayout是谷歌官方的侧滑菜单布局,drawerlayout下的第一个布局被视为主布局,第二个被视为左滑菜单,第三个为右滑菜单

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.AppBarOverlay" >
    </android.support.v7.widget.Toolbar>

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        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="@+id/fl_realcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"></FrameLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eee"/>
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff">
                <android.support.v4.app.FragmentTabHost
                    android:id="@+id/tab_host"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <ImageView
                    android:id="@+id/iv_add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/btn_quickoption_nor"
                    android:layout_gravity="center"/>
            </FrameLayout>


        </LinearLayout>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity ="left"
            android:background="#eee"
            ></FrameLayout>
    </android.support.v4.widget.DrawerLayout>

2、在Java代码中通过ActionBarDrawerToggle关联toolbar和drawerlayout

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToolbar =(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBarDrawerToggle = new ActionBarDrawerToggle(this,
        mDrawerLayout, mToolbar, R.string.app_name, R.string.action_settings);
mActionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

3、在主题样式文件中设置为NoActionBar,防止actionbar和toolbar冲突,style.xml文件如下

<resources>
    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
        <!-- 去掉actionbar以防止和toolbar冲突-->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <!-- toolbar 上文字的颜色-->
        <item name="android:textColorPrimary">@color/white</item>
        <!-- android 5.0 Material Design标题栏样式-->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- 设置菜单栏控制图标的颜色为白色 -->
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>

    <!-- 左边的箭头指示-->
    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>
</resources>

4、fragmentTabhost实现fragment切换
(1) 新建一个Indicator枚举类封装页面底部的tab键

public enum Indicator {
    Main(R.drawable.tab_icon_new,"Fragment1", MainFragment.class),
    NEWS(R.drawable.tab_icon_tweet,"Fragment2", NewsFragment.class),
    QUICK(R.drawable.tab_icon_new,"Fragment3", MainFragment.class),
    FIND(R.drawable.tab_icon_explore,"Fragment4", FindFragment.class),
    ME(R.drawable.tab_icon_me,"Fragment5", MeFragment.class);
    int resIcon;
    String tabName;
    Class clz;
    Indicator(int resIcon,String tabName,Class clz){
        this.resIcon = resIcon;
        this.tabName = tabName;
        this.clz = clz;

    }

    public Class getClz() {
        return clz;
    }

    public void setClz(Class clz) {
        this.clz = clz;
    }

    public int getResIcon() {
        return resIcon;
    }

    public void setResIcon(int resIcon) {
        this.resIcon = resIcon;
    }

    public String getTabName() {
        return tabName;
    }

    public void setTabName(String tabName) {
        this.tabName = tabName;
    }
}
private void initTabHost() {
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        fragmentTabHost.setup(MainActivity.this,getSupportFragmentManager(),R.id.fl_realcontent);
        Indicator []indicators = Indicator.values();
        for(int i=0;i<indicators.length;i++){
            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(indicators[i].getTabName());
            View view = getIndicatorView(indicators[i]);
            //中间的按钮点击跳转到activity而不是fragment
            if(i == 2){
                view.setVisibility(View.INVISIBLE);
            }
            tabSpec.setIndicator(view);
            fragmentTabHost.addTab(tabSpec,indicators[i].getClz(),null);
        }
        //设置fragmenttabhost的间隙为0
        fragmentTabHost.getTabWidget().setShowDividers(0);
        ImageView ivAdd = (ImageView) findViewById(R.id.iv_add);
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"test",Toast.LENGTH_SHORT).show();
            }
        });
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值