簡體   English   中英

在android中的操作欄上設置導航抽屜

[英]set navigation drawer on actionbar in android

我已經創建了一個導航抽屜。我想在狀態欄下面顯示抽屜。它可以正常工作。但抽屜在版本5及以上的狀態欄上方打開。我想將抽屜設置為類似於第二個圖像。如何在所有版本的狀態欄下方的動作(即)上設置導航抽屜

<?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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="close"
   >

    <include
        layout="@layout/emp_app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="start"

        android:fitsSystemWindows="true"

        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"
        android:background="@drawable/menubg"
        app:headerLayout="@layout/emp_header"
        app:menu="@menu/activity_emp_drawer" />


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

` 抽屜在通用5+手機上方的操作欄上方打開

圖像2

在此輸入圖像描述

嘗試將android:fitsSystemWindows="true"移除到DrawerLayout或將其DrawerLayout false,如android:fitsSystemWindows="false" ,這可能對您android:fitsSystemWindows="false"

用以下代碼替換您的代碼:

<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/emp_app_bar_home"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:itemTextColor="@color/black"
    app:itemIconTint="@color/black"
    android:background="@drawable/menubg"
    app:headerLayout="@layout/emp_header"
    app:menu="@menu/activity_emp_drawer" />

我所做的是找到狀態欄的高度並抵消它。

private NavigationView mNavView;
private View mHeaderView;
//...
mNavView = (NavigationView) findViewById(R.id.navView);
//...
mHeaderView = mNavView.getHeaderView(0);
//...
LinearLayout mHeaderLayout = (LinearLayout) HeaderView.findViewById(R.id.headerLayout);
//..
mHeaderLayout.setPadding(0, getStatusBarHeight(), 0, 0);

獲取狀態欄高度的功能是:

public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
}

希望有所幫助。

PS。 找到狀態欄的高度我很久以前就把它拿到了別的地方......

android:fitsSystemWindows="true" emp_header你在app:headerLayout="@layout/emp_header"提到的app:headerLayout="@layout/emp_header"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM