简体   繁体   English

没有操作栏的 Android NavigationDrawer

[英]Android NavigationDrawer without Actionbar

I'm trying to create a Navigation Drawer in my app, without the ActionBar.我正在尝试在我的应用程序中创建一个导航抽屉,而没有 ActionBar。 My previous attempt using NoActionBar themes in the styles works but now I can't swipe to open the navigation menu.我之前在样式中使用 NoActionBar 主题的尝试有效,但现在我无法滑动以打开导航菜单。 I need something like this:我需要这样的东西:

How can I go about this?我该怎么办?

Just remove the toolbar from the app_bar_home.xml.只需从 app_bar_home.xml 中删除toolbar
Add an ImageView, set it to that position and set an onClickListener to it.添加一个 ImageView,将其设置到该位置并为其设置一个onClickListener
Use this code for ImageView.将此代码用于 ImageView。

    final ImageView toolbar1 = (ImageView) findViewById(R.id.Toolbar1); 
    final DrawerLayout drawer =  findViewById(R.id.drawer_layout);
    toolbar1.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View view) {
             drawer.openDrawer(GravityCompat.START);
         }
     });

Change your theme related to this.更改与此相关的主题。

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimaryDark">@android:color/transparent</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:navigationBarColor" tools:targetApi="21">@color/navColor</item>
    <item name="android:windowDrawsSystemBarBackgrounds" tools:targetApi="21">true</item>
    <item name="android:statusBarColor" tools:targetApi="21">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus" tools:targetApi="21">true</item>
    </style>

    <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    </style>

This works great as I'm using it.这很好用,因为我正在使用它。

I have done this same type of Navigation drawer in my previous project我在之前的项目中做过同样类型的导航抽屉

Just apply some changes in style.xml and layout.xml file只需在style.xmllayout.xml文件中应用一些更改

style.xml样式文件

     <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppTheme.ActionBar.Transparent" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="colorPrimary">@android:color/transparent</item>
        <item name="colorPrimaryDark">@android:color/transparent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

    </style>

</resources>

Note: Put AppTheme.ActionBar.Transparent named theme on both file style.xml and style.xml(v21)注意:AppTheme.ActionBar.Transparent命名主题放在文件 style.xml 和 style.xml(v21) 上

Manifest.xml清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user22.nevigationdrawer">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.ActionBar.Transparent">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • Make changes on app_bar_yourlayout_name.xmlapp_bar_yourlayout_name.xml 进行更改

app_bar_main.xml app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    android:fitsSystemWindows="true"
    tools:context="com.example.user22.nevigationdrawer.MainActivity">

    <ImageView
        android:id="@+id/img_hotel_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher"
        app:layout_collapseMode="parallax" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolBar_hotel_home"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed|exitUntilCollapsed">

            <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" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

if You want to change the NavigationDrawer ActionBar icon than just change in your NavigationDrawer main java file如果您想更改 NavigationDrawer ActionBar 图标,而不仅仅是更改 NavigationDrawer 主 Java 文件

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
        toggle.setDrawerIndicatorEnabled(false);
        toggle.setHomeAsUpIndicator(R.mipmap.ic_launcher);

toggle.setToolbarNavigationClickListener toggle.setToolbarNavigationClickListener

toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                drawer.openDrawer(GravityCompat.START);
            }
        }
    });

Note: Remove onBackPressed() method from your java file if you change icon of Navigation Drawer注意:如果您更改导航抽屉的图标,请从您的 java 文件中删除 onBackPressed() 方法

在此处输入图片说明

i have done also the same thing but the problem is when i am in main activity and after that I click on navigation views item then it shows fragment above main activity thats for I can access main activity Views From this fragment. 我也做了同样的事情,但是问题是当我处于主要活动中之后,我单击导航视图项目,然后它显示了主要活动上方的片段,因为我可以从该片段访问主要活动视图。 the main activity button are working above fragments i don't want this, plz help 主要活动按钮在片段上方工作我不想要这个,请帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM