Android 多窗口启动器侧边栏

问题描述

我希望这个 drawerLayout(波纹管照片)总是像三星多窗口侧边栏一样打开。

直到现在我可以在应用程序中打开这个 drawerLayout(sidebar)。请给出能够从任何地方(我的意思是从任何其他应用程序甚至从设备的主屏幕)打开此 draweLayout(侧边栏)的说明。

提前致谢。

This this the sidebar

这是我的drayerLayout XML 文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    android:background="@color/cardview_light_background">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swipe Right"
        android:textSize="15dp"
        android:textColor="#D50000"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open App In\nMultiWIndow Mode"
        android:textSize="30sp"
        android:gravity="center"
        android:textStyle="bold"
        android:textColor="@color/black"/>


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            tools:context=".MainActivity2">

            <ListView
                android:id="@+id/listView"
                android:layout_width="280dp"
                android:layout_height="0dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

解决方法

这不能通过在每个活动中做一些修改来实现全局性

您可以创建一个新类,该类将具有初始化导航的方法,将导航视图移动到单独的 xml 文件并将该文件包含在每个活动中,并在每个活动中调用初始化方法

您可以尝试使用此库 https://github.com/mikepenz/MaterialDrawer 将大多数 xml 代码移至 java,这样您就不必在每个活动中添加 DrawerLayout,但您可以自行选择,如果可以,最好跳过使用 3rd 方库

如果您使用的是图书馆,那么您可以按照以下示例进行操作

常量.java

private void setupNavigationDrawer() {
    ProfileDrawerItem section = new ProfileDrawerItem()
            .withSetSelected(false)
            .withSetSelected(false)
            .withEnabled(false);
    PrimaryDrawerItem joyType0DrawerItem = new PrimaryDrawerItem().withName("Basic Gamepad").withTag(KEY_BASIC_GAMEPAD_1).withLevel(2);
    PrimaryDrawerItem joyType1DrawerItem = new PrimaryDrawerItem().withName("Assault Horizon").withTag(KEY_ASSAULT_HORIZON).withLevel(2);
    PrimaryDrawerItem joyType2DrawerItem = new PrimaryDrawerItem().withName("Awesomenauts").withTag(KEY_AWESOMENAUTS).withLevel(2);
    PrimaryDrawerItem joyType3DrawerItem = new PrimaryDrawerItem().withName("BombSquad").withTag(KEY_BOMBSQUAD).withLevel(2);
    PrimaryDrawerItem joyType4DrawerItem = new PrimaryDrawerItem().withName("Final Fantasy XIII").withTag(KEY_FINAL_FANTASY_XIII).withLevel(2);
    SectionDrawerItem sectionDrawerItem = new SectionDrawerItem().withName("Joystick Type").withTextColorRes(R.color.colorAccent);
    PrimaryDrawerItem deviceDrawerItem = new PrimaryDrawerItem().withName("Device Connection").withTag(KEY_DEVICE_CONNECTION).withIcon(GoogleMaterial.Icon.gmd_devices);
    PrimaryDrawerItem settingsDrawerItem = new PrimaryDrawerItem().withName("Settings").withTag("settings").withIcon(GoogleMaterial.Icon.gmd_settings);

    ndMenu = new DrawerBuilder()
            .withActivity(this)
            .withToolbar(tbMain)
            .withCloseOnClick(true)
            .withFireOnInitialOnClick(true)
            .withSelectedItemByPosition(6)
            .withDelayDrawerClickEvent(300)
            .withDisplayBelowStatusBar(true)
            .withTranslucentStatusBar(false)
            .withOnDrawerItemClickListener(this)
            .addDrawerItems(section,deviceDrawerItem,settingsDrawerItem,sectionDrawerItem,joyType0DrawerItem,joyType1DrawerItem,joyType2DrawerItem,joyType3DrawerItem,joyType4DrawerItem)
            .build();

    findViewById(R.id.nav_button).setOnClickListener(new 
     View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
        // Check these functions from library example to check drawer state and open close drawer
        if(ndMenu.isDrawerOpen){
           ndMenu.closeDrawer();
         } else {
           ndMenu.openDrawer();
         }
        });
}

MainActivity.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

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


<LinearLayout>

nav_view.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    orientation="horizontal">

    <Image
      id="@+id/nav_button"
      android:layout_width="30dp"
      android:layout_height="30dp"/>


<LinearLayout>