Android--DrawerLayout的基本用法

DrawerLayout提供给了一个左右滑动抽屉的效果。一般把DrawerLayout设为根布局,然后分别配置主页面、左抽屉和右抽屉。

xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <Button
            android:text="open left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/open_left_button"
            android:layout_gravity="center"/>

        <Button
            android:text="open right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/open_right_button"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="150dp"/>
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:gravity="center_horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是左边栏"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/right_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="#111"
        android:gravity="center_horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是右边栏"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
    </RelativeLayout>

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

注意:

1.xml中,主页面需要放在最上面,如上面的FrameLayout子节点。

2.左右抽屉通过layout_gravity的start或end属性来确定(start为左抽屉、end为右抽屉),如上面的两个RelativeLayout子节点。


java:

package com.example.goodix.coordinator;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Snackbar mSnackbar = null;
    private DrawerLayout mDrawerLayout = null;
    private Button openLeftBtn = null;
    private Button openRightBtn = null;

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

        initView();

    }

    private void initView() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //关闭手势滑动
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                Log.d("lyt", "DrawSlide!!");
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.d("lyt", "DrawOpen!!");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.d("lyt", "DrawClosed!!");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.d("lyt", "DrawStateChanged!!");
            }
        });

        openLeftBtn = (Button) findViewById(R.id.open_left_button);
        openLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
                mSnackbar = Snackbar.make(mDrawerLayout, "打开抽屉", Snackbar.LENGTH_SHORT);
            }
        });

        openRightBtn = (Button) findViewById(R.id.open_right_button);
        openRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.RIGHT);
                mSnackbar = Snackbar.make(mDrawerLayout, "打开抽屉", Snackbar.LENGTH_SHORT);
            }
        });


    }
}
1.抽屉默认打开手势滑动。

2.mDrawerLayout的openDrawer和closeDrawer方法可打开或关闭抽屉。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值