android侧滑菜单实现

 

android侧滑菜单实现_第1张图片        android侧滑菜单实现_第2张图片

  1. layout_toolbar

xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:id="@+id/toolbar"
   
android:background="?attr/colorPrimary"
   
android:minHeight="?attr/actionBarSize"
   
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
   
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
   
>
android.support.v7.widget.Toolbar>

 

  1. layout_drawer
xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/main_drawer_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/transparent">



    

    <RelativeLayout

        android:id="@+id/main_content_frame_parent"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@android:color/transparent"

        android:gravity="center">



        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_gravity="center"

            android:onClick="openLeftLayout"

            android:text="左边" />



        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_gravity="center"

            android:layout_marginLeft="100dp"

            android:onClick="openRightLayout"

            android:text="右边" />



    RelativeLayout>



    

    <RelativeLayout

        android:id="@+id/main_left_drawer_layout"

        android:layout_width="240dp"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:background="@color/colorPrimary"

        android:paddingTop="50dp">



        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:text="左边菜单测试" />



    RelativeLayout>



    

    <RelativeLayout

        android:id="@+id/main_right_drawer_layout"

        android:layout_width="240dp"

        android:layout_height="match_parent"

        android:layout_gravity="end"

        android:background="@color/colorPrimary"

        android:paddingTop="50dp">



        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:text="右边菜单测试" />

    RelativeLayout>





android.support.v4.widget.DrawerLayout>

 

  1. activity_main
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:orientation="vertical">



    <include layout="@layout/layout_toolbar" />



    <include layout="@layout/layout_drawer" />

LinearLayout>

 

  1. styles.xml
<style name="NoActionBar" parent="Theme.AppCompat.NoActionBar">

    <item name="android:windowActionBar">falseitem>

    <item name="android:windowNoTitle">trueitem>

style>

 

5.activity在manifest里面的theme要设成android:theme="@style/NoActionBar"

6.MainActivity.class

import android.graphics.Color;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    // 抽屉菜单对象
    private ActionBarDrawerToggle drawerbar;
    public DrawerLayout drawerLayout;
    private RelativeLayout main_left_drawer_layout, main_right_drawer_layout;

    private Toolbar toolbar;

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

        initLayout();
        initEvent();
    }

    public void initLayout() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);

        //设置菜单内容之外其他区域的背景色
        drawerLayout.setScrimColor(Color.TRANSPARENT);

        //左边菜单
        main_left_drawer_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout);
        //右边菜单
        main_right_drawer_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);

    }

    //设置开关监听
    private void initEvent() {
        drawerbar = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, toolbar, R.string.open, R.string.close) {
            //菜单打开
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            // 菜单关闭
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
//        drawerLayout.setDrawerListener(drawerbar);
        drawerLayout.addDrawerListener(drawerbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        drawerbar.syncState();
    }

    //左边菜单开关事件
    public void openLeftLayout(View view) {
        if (drawerLayout.isDrawerOpen(main_left_drawer_layout)) {
            drawerLayout.closeDrawer(main_left_drawer_layout);
        } else {
            drawerLayout.openDrawer(main_left_drawer_layout);
        }
    }

    // 右边菜单开关事件
    public void openRightLayout(View view) {
        if (drawerLayout.isDrawerOpen(main_right_drawer_layout)) {
            drawerLayout.closeDrawer(main_right_drawer_layout);
        } else {
            drawerLayout.openDrawer(main_right_drawer_layout);
        }
    }
}

你可能感兴趣的:(IT,android)