Android中级:ActionBar + DrawerLayout实现侧滑菜单

本文详细介绍如何使用Android中的DrawerLayout和ActionBarDrawerToggle组件实现侧滑菜单功能。通过实例代码展示了初始化控件、设置ActionBar以及添加监听器的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关介绍

图:

这里写图片描述

有关的类:

  1. ActionBar: V7
  2. DrawerLayout:v4
  3. ActionBarDrawerToggle:v4

步骤:

  1. 获取ActionBar + DrawerLayout控件
  2. 设置ActionBar
  3. 创建ActionBarDrawerToggle对象,并同步
  4. 给添加DrawerLayout监听

代码:

activity_main.xml

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

    <!--内容页 要放在上面-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="内容页"
            android:textSize="25sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="侧拉页"
            android:textSize="25sp"/>
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

MainActivity.java

package com.cqc.drawerlayoutdemo1;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toggle;

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

        initView();
        initActinBar();
    }

    private void initView() {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    }

    /**
     * 有关的类:ActionBar + ActionBarDrawerToggle + DrawerLayout
     */
    private void initActinBar() {
        //1 获取ActionBar对象并设置属性
        ActionBar actionBar = getSupportActionBar();
        actionBar.setLogo(R.mipmap.ic_launcher);//没有图标
//        actionBar.setIcon(R.mipmap.ic_launcher);//图标
        actionBar.setTitle(getString(R.string.app_name));// 设置标题
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
//        actionBar.setIcon(R.drawable.ic_drawer_am);

        //2 创建对象,ActionBarDrawerToggle设置导航图标,并同步
        toggle = new ActionBarDrawerToggle(this,drawerLayout,R.drawable.ic_drawer_am,0,0);
        toggle.syncState();

        //给drawerlayout添加监听
        //方法一:
        drawerLayout.setDrawerListener(toggle);
        //方法二:
//        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
//            @Override
//            public void onDrawerSlide(View drawerView, float slideOffset) {
//                toggle.onDrawerSlide(drawerView,slideOffset);
//            }
//
//            @Override
//            public void onDrawerOpened(View drawerView) {
//                toggle.onDrawerOpened(drawerView);
//            }
//
//            @Override
//            public void onDrawerClosed(View drawerView) {
//                toggle.onDrawerOpened(drawerView);
//            }
//
//            @Override
//            public void onDrawerStateChanged(int newState) {
//                toggle.onDrawerStateChanged(newState);
//            }
//        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    //若要侧拉栏出现,还需要添加下面一行代码
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        toggle.onOptionsItemSelected(item);
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值