【问题标题】:Android multi window launcher sidebarAndroid 多窗口启动器侧边栏
【发布时间】:2021-07-28 07:02:43
【问题描述】:

我希望此抽屉布局(下图)始终像三星多窗口侧边栏一样打开。

到目前为止,我可以在应用程序中打开此抽屉布局(侧边栏)。请给出能够从任何地方(我的意思是从任何其他应用程序甚至是设备的主屏幕)打开此draweLayout(侧边栏)的说明。

提前致谢。

这是我的 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>

【问题讨论】:

  • 指出。谢谢。

标签: java android navigation-drawer sidebar slidingdrawer


【解决方案1】:

这无法通过让你在每个活动中进行一些修改来实现全局化

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

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

如果您使用库而不是这里的一些示例,您可以遵循

常量.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>

【讨论】:

  • 但我说的是像三星多窗口任务栏这样的侧边栏。每当用户进行设置时,他就可以从任何应用程序或设备的主屏幕访问侧边栏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2021-07-06
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多