工具栏上的汉堡包图标充当后退按钮

帕特

我试图用androidx库在我的应用程序中实现导航抽屉。到目前为止,该应用程序在左上角显示汉堡包图标,但是要访问抽屉,我必须向右滑动。如果单击该图标,它将转到上一个活动。是否因为该活动不是主要活动?我怎样才能解决这个问题?先感谢您。

这是我的活动

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
        ActionBarDrawerToggle t = new ActionBarDrawerToggle(this, drawerLayout,R.string.Open, R.string.Close);
        drawerLayout.addDrawerListener(t);
        t.syncState();

        NavigationView nv = findViewById(R.id.navigationView);
        nv.setNavigationItemSelectedListener(item -> {
            int id = item.getItemId();
            switch(id)
            {
                case R.id.action_open_list:
                    break;
                case R.id.action_closed_list:
                    Intent closedListIntent = new Intent(this, ClosedListActivity.class);
                    startActivity(closedListIntent);
                    break;
                default:
                    return true;
            }


            return true;

        });
    }

我的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <TextView
            android:id="@+id/tv_error_message_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="@string/error_message_cannot_connect"
            android:textSize="20sp"
            android:visibility="invisible" />

        <ProgressBar
            android:id="@+id/pb_loading_indicator"
            android:layout_width="42dp"
            android:layout_height="42dp"
            android:layout_gravity="center"
            android:visibility="invisible" />

    </FrameLayout>
    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/navigationView"/>

</androidx.drawerlayout.widget.DrawerLayout>
阿萨杜扎曼医师

Toolbar需要成为DrawerLayout一起工作的孩子

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <androidx.appcompat.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" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <TextView
                android:id="@+id/tv_error_message_display"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:text="@string/error_message_cannot_connect"
                android:textSize="20sp"
                android:visibility="invisible" />

            <ProgressBar
                android:id="@+id/pb_loading_indicator"
                android:layout_width="42dp"
                android:layout_height="42dp"
                android:layout_gravity="center"
                android:visibility="invisible" />

        </FrameLayout>
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/navigationView"/>

</androidx.drawerlayout.widget.DrawerLayout>

然后在您的活动中

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id. drawerLayout);
    ActionBarDrawerToggle t = new ActionBarDrawerToggle(
            this, drawerLayout, toolbar, R.string.Open, R.string.Close);
    drawerLayout.addDrawerListener(t);
    t.syncState();

    ....
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

导航抽屉:无法在儿童活动的工具栏上显示后退图标,而不显示汉堡包图标

来自分类Dev

单击工具栏上的汉堡包图标不会打开导航抽屉

来自分类Dev

在Android工具栏中删除汉堡包(菜单)图标后的大填充?

来自分类Dev

如何使用自定义可绘制对象替换用于Android工具栏上的ActionBarToggle的汉堡包图标?

来自分类Dev

Bootstrap,汉堡包图标在导航栏中不响应

来自分类Dev

将汉堡包按钮添加到导航栏

来自分类Dev

如何居中工具栏后退按钮?

来自分类Dev

ActionBarDrawerToggle-以编程方式将汉堡包图标切换为后退图标

来自分类Dev

如何在Android材质组件中更改工具栏的后退按钮图标

来自分类Dev

更改导航抽屉汉堡包图标

来自分类Dev

汉堡包图标加载为“X”

来自分类Dev

xamarin.forms 汉堡包图标

来自分类Dev

MvvmCross-将“汉堡包”菜单更改为“后退”按钮

来自分类Dev

设置工具栏颜色和导航栏后退按钮

来自分类Dev

如何设置表单工具栏后退图标的颜色?

来自分类Dev

Android上的工具栏图标着色

来自分类Dev

无法在工具栏上显示图标

来自分类Dev

Xamarin Forms Shell仍然可以删除导航栏,但保留汉堡包图标吗?

来自分类Dev

汉堡包按钮不在正确的位置

来自分类Dev

如何实现android工具栏后退按钮

来自分类Dev

工具栏中的后退按钮不起作用

来自分类Dev

工具栏后退按钮导致返回父活动的问题

来自分类Dev

启用“后退按钮”时工具栏标题不在中心

来自分类Dev

工具栏正在NavigationView中删除我的后退按钮

来自分类Dev

当按下工具栏的后退按钮时

来自分类Dev

片段工具栏后退按钮未显示

来自分类Dev

工具栏后退按钮和 onBackPressed 方法的区别

来自分类Dev

使用样式在工具栏中显示后退按钮

来自分类Dev

如何对齐工具栏上的按钮?

Related 相关文章

  1. 1

    导航抽屉:无法在儿童活动的工具栏上显示后退图标,而不显示汉堡包图标

  2. 2

    单击工具栏上的汉堡包图标不会打开导航抽屉

  3. 3

    在Android工具栏中删除汉堡包(菜单)图标后的大填充?

  4. 4

    如何使用自定义可绘制对象替换用于Android工具栏上的ActionBarToggle的汉堡包图标?

  5. 5

    Bootstrap,汉堡包图标在导航栏中不响应

  6. 6

    将汉堡包按钮添加到导航栏

  7. 7

    如何居中工具栏后退按钮?

  8. 8

    ActionBarDrawerToggle-以编程方式将汉堡包图标切换为后退图标

  9. 9

    如何在Android材质组件中更改工具栏的后退按钮图标

  10. 10

    更改导航抽屉汉堡包图标

  11. 11

    汉堡包图标加载为“X”

  12. 12

    xamarin.forms 汉堡包图标

  13. 13

    MvvmCross-将“汉堡包”菜单更改为“后退”按钮

  14. 14

    设置工具栏颜色和导航栏后退按钮

  15. 15

    如何设置表单工具栏后退图标的颜色?

  16. 16

    Android上的工具栏图标着色

  17. 17

    无法在工具栏上显示图标

  18. 18

    Xamarin Forms Shell仍然可以删除导航栏,但保留汉堡包图标吗?

  19. 19

    汉堡包按钮不在正确的位置

  20. 20

    如何实现android工具栏后退按钮

  21. 21

    工具栏中的后退按钮不起作用

  22. 22

    工具栏后退按钮导致返回父活动的问题

  23. 23

    启用“后退按钮”时工具栏标题不在中心

  24. 24

    工具栏正在NavigationView中删除我的后退按钮

  25. 25

    当按下工具栏的后退按钮时

  26. 26

    片段工具栏后退按钮未显示

  27. 27

    工具栏后退按钮和 onBackPressed 方法的区别

  28. 28

    使用样式在工具栏中显示后退按钮

  29. 29

    如何对齐工具栏上的按钮?

热门标签

归档