没有后退选项显示

时间:2018-03-24 20:46:15

标签: android android-layout android-studio

这是我的navigation.class,也是我的主要类。它在我的导航抽屉里面和allfragments(片段)

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation33);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        toolbar.setNavigationIcon(R.drawable.ic_flash);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        cooLayout=(ConstraintLayout)findViewById(R.id.cooLayout);
        FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.fabId);
        myFab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(navigation33.this,Post.class));
            }
        });
        auth = FirebaseAuth.getInstance();
//        db = FirebaseFirestore.getInstance();

        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user == null) {
                    startActivity(new Intent(navigation33.this, login_user.class));
                    finish();
                }
            }
        };
         drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);

        toggle.syncState();
        setTitle("Appx");

         navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        fragmentManager = getSupportFragmentManager();

        myFragmentTransaction = fragmentManager.beginTransaction();
        myFragmentTransaction.replace(R.id.cooLayout, new AllFragments()).commit();
    }

我在这里有答案,只需添加getSupportActionBar().setDisplayHomeAsUpEnabled(true);即可解决问题,但不是我的问题。

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/orange"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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

默认情况下不应该这样,我在这里缺少什么? 提前致谢! 怎么样 Like This 我多么想要它 enter image description here

1 个答案:

答案 0 :(得分:1)

尝试设置工具栏的图标。

    toolbar.setNavigationIcon(R.drawable.ic_back_arrow);
    toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onSupportNavigateUp();
        }
    });

修改

我在这里进行了测试,如果你添加可以通过在你的切换对象上调用它来控制显示或隐藏后退箭头:

toggle.setDrawerIndicatorEnabled(false);

然后再次更新工具栏

    public void onBackPressed() {
...
        toggle.setDrawerIndicatorEnabled(true);

所以,如果你这样做:

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
 toggle.setDrawerIndicatorEnabled(false);

不要忘记添加:

 @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

仅在以下情况下有效:

    myFragmentTransaction.addToBackStack(null);

你不会得到汉堡包按钮而是向后箭头。因此,您需要将toggle成为一个成员变量,以便全局访问它并控制何时显示或隐藏后退箭头。