toolbar+drawerlayout仿知乎侧栏~~步骤

toolbar是android.support.v7.widget的

drawerLayout是android.support.v4的,记得引用

1.首先修改一下app的theme,在values/styles.xml中加入两个<item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
提醒:是windowActionBar不是android:windowActionBar,版本更新了,带android的会爆错。

为什么要修改呢?因为我们要的是toolbar,所以要屏蔽掉actionbar

2.然后在xml中加入<android.support.v7.widget.Toolbar>以及<<android.support.v4.widget.DrawerLayout>结点,侧栏及内容放在Drawerlayout中,侧栏设置layout_gravity为start或者left,当然也可以同时加入一个右边的侧栏。

类似这样

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0099FF">

    </android.support.v7.widget.Toolbar>
    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        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">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="内容"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#fff">
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/listview"
                android:divider="@null"
                ></ListView>
        </LinearLayout>


    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

3.在values/strings.xml文件中加入两个

<string name="drawer_open">Drawer Open</string>
<string name="drawer_close">Drawer Close</string>

4.

Toolbar toolbar;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;

toolbar=(Toolbar)findViewById(R.id.toolbar);

        //toolbar.inflateMenu(R.menu.menu_main);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        drawerLayout=(DrawerLayout)findViewById(R.id.drawerlayout);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,
                R.string.drawer_open,R.string.drawer_close);
        actionBarDrawerToggle.syncState();
        drawerLayout.setDrawerListener(actionBarDrawerToggle);


猜你喜欢

转载自blog.csdn.net/ccc905341846/article/details/50091087
今日推荐