Toolbar 加 DrawerLayout 简单使用实现侧滑菜单

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dw"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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"
    tools:context="com.example.administrator.toolbaranddrawerlayout.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ></android.support.v7.widget.Toolbar>
    </RelativeLayout>
    
    
    <!--DrawerLayout侧滑中设置左侧滑动设置属性为: android:layout_gravity="start" ,
    设置右侧滑动属性为:android:layout_gravity="end" 本文设置的为右侧滑动-->
        <RelativeLayout
            android:id="@+id/rl"
            android:background="@color/colorPrimary"
            android:layout_gravity="end"
            android:layout_width="200dp"
            android:layout_height="match_parent"></RelativeLayout>
</android.support.v4.widget.DrawerLayout>

设置Toolbar:Toolbar是安卓自带的不需要导包 但是需要把AndroidManifest 中Android:theme属性改变一下否则Toolbar不会显示出来

//替换前
android:theme="@style/AppTheme"

//替换后
android:theme="@style/Theme.AppCompat.Light.NoActionBar"

Mainactivity中实现效果

package com.example.administrator.toolbaranddrawerlayout;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private DrawerLayout dw;
    private RelativeLayout right;

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

        initView();
        //添加图标
        toolbar.setLogo(R.drawable.ic_launcher_background);
        toolbar.setTitle("主标题");
        toolbar.setSubtitle("副标题");
        setSupportActionBar(toolbar);
        //添加左侧点击图标(注意需要在setSupportActionBar(toolbar)之后才有效果)
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        //为左侧图标设置点击事件
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断该控件是否已经打开
                if (dw.isDrawerOpen(right)){
                    dw.closeDrawer(right);
                }else {
                    dw.openDrawer(right);
                }
            }
        });
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        dw = (DrawerLayout) findViewById(R.id.dw);
        right = (RelativeLayout) findViewById(R.id.rl);

    }
}

猜你喜欢

转载自blog.csdn.net/jie1175623465/article/details/80263429
今日推荐