【问题标题】:Android Studio navigation drawer with fragments. Tool Bar Hidden in next fragment Activity or page带有片段的 Android Studio 导航抽屉。工具栏隐藏在下一个片段活动或页面中
【发布时间】:2021-04-09 18:12:15
【问题描述】:

我正在尝试使用片段创建导航抽屉。我想隐藏/删除下一个片段中的工具栏。我想要没有工具栏的完整空片段。但是每当我尝试我的导航抽屉时,它就无法正常工作。我想要带有 Toolbar+Navigation_Drawer 的前 3 页菜单(片段)和接下来的两个片段活动,只有一个导航抽屉(不包括工具栏)

Below is my code activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">


<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="#ffffff"
        />
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>
    <FrameLayout
        android:id="@+id/fragment_container2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

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

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




</androidx.drawerlayout.widget.DrawerLayout>

这是 MainActivity.java

    package com.example.guvi;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.ActionBarDrawerToggle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import androidx.core.view.GravityCompat;
    import androidx.drawerlayout.widget.DrawerLayout;
    
    import android.annotation.SuppressLint;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MenuItem;
    
    import com.google.android.material.navigation.NavigationView;
    
    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    
        private DrawerLayout drawer;
        Toolbar toolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
    
           toolbar = findViewById(R.id.toolbar);
           setSupportActionBar(toolbar);
    
           drawer = 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();
    
          this.setTitle("");
          getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CoursesFragment()).commit();
    
    
            NavigationView naview = findViewById(R.id.nav_view);
            naview.setNavigationItemSelectedListener(this);
    
    
        }
    
        @Override
        public void onBackPressed() {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }
    
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    
            switch (item.getItemId()){
                case R.id.nav_course:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CoursesFragment()).commit();
                    break;
                case R.id.nav_invfriends:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new InviteFragment2()).commit();
                    break;
                case R.id.nav_contactus:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ContactFragment()).commit();
                    break;
    
            }
    
            drawer.closeDrawer(GravityCompat.START);
    
            return true;
        }
    
    
    }

【问题讨论】:

    标签: java android android-fragments navigation-drawer android-studio-3.0


    【解决方案1】:

    你可以做的是:

    • 在您的活动(其中包含片段的“基础”)中,在 onCreate() 调用中将工具栏设置为隐藏在 MainActivity.java 中

    'toolbar.setVisibility(View.INVISIBLE)' 如果你想隐藏它 要么 'toolbar.setVisibility(View.GONE)' 如果你想让它从你的活动中消失

    • 我不知道你是否也将你的xml片段调用到activity.xml中,但我建议将它们分开放在不同的xml中,这样更容易管理。

    • 在您的 MainActivity.java 中,只要您想要任何带有工具栏的片段只需调用“toolbar.setVisibility(View.VISIBLE)”,我的意思是为每个片段使用单独的类

    我希望它有效

    【讨论】:

    • 它没有帮助我
    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多