android--actionBar

本文介绍了一种使用Android的DrawerLayout实现侧滑菜单的方法,包括XML布局配置及MainActivity中的ActionBar设置过程。通过该方法可以轻松地在应用中加入侧滑菜单功能。

xml文件

 
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ui.ht.rscja.com.testitem.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        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:background="@color/colorPrimary"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#AAAAAA"/>
        <FrameLayout
            android:background="#66FF66"
            android:id="@+id/frame_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/fragment_head"
        app:menu="@menu/left_menu"/>
</android.support.v4.widget.DrawerLayout>

MainActivity.java
   //上面的anctionbar
    private void setBar(){
        setSupportActionBar(toolbar);//注意将主题设置为NoActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
        actionBarDrawerToggle.syncState();//← 变成 ==
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(this,R.color.colorBackgrund));
    }
  // 创建Actionbar menu 菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }
 //处理menu菜单里面的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case  R.id.menu_look:
                break;
            case R.id.menu_set:
                break;
        }
        return true;//return super.onOptionsItemSelected(item);
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值