DrawerLayout ---抽屉效果的导航菜单

这里写图片描述

支持需要:android-support-v4.jar
首先, DrawerLayout这个类是在Support Library里的,需要加上android-support-v4.jar这个包。
然后程序中用时在前面导入import android.support.v4.widget.DrawerLayout;
如果找不到这个类,首先用SDK Manager更新一下Android Support Library,然后在Android SDK\extras\android\support\v4路径下找到android-support-v4.jar,复制到项目的libs路径,将其 Add to Build Path.

使用方法:
1.在主布局文件中添加简单的DrawerLayout,可同时添加左右两个抽屉布局,如下:

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

    <!--此处为主界面布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:theme="@style/AppTheme.AppBarOverlay">

        <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.PopupOverlay" />
        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="testbtn"/>

    </LinearLayout>

    <!--此处为抽屉布局,因指定android:layout_gravity="left|bottom",故为左侧抽屉-->
    <LinearLayout
        android:id="@+id/mleft_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|bottom"
        android:background="#cccccc"
        >

        <TextView
            android:id="@+id/fab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="40dp"
            android:background="#FF98DED7"
            android:text="test" />
    </LinearLayout>

    <!--此处为抽屉布局,因指定android:layout_gravity="right|bottom",故为右抽屉-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="right|bottom"
        >

        <TextView
            android:id="@+id/fabd"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:layout_marginTop="40dp"
            android:layout_marginBottom="40dp"
            android:background="#FF98DED7"
            android:text="test" />
    </LinearLayout>

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

2.java代码中对事件进行监听,可通过按钮的点击事件来控制菜单的展与合,

package com.example.demo.myapplication;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mdr;
    private LinearLayout mleft;
    private Button mbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mdr = (DrawerLayout)findViewById(R.id.main_drawer_layout);
        mleft =(LinearLayout)findViewById(R.id.mleft_drawer);
        mbtn = (Button)findViewById(R.id.btn);
        mbtn.setOnClickListener(mclicklisenter);

    }

    View.OnClickListener mclicklisenter = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mdr.isDrawerOpen(mleft)){
                mdr.closeDrawer(mleft);
            } else {
                mdr.openDrawer(mleft);
            }
        }
    };
}

详情参考
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值