所有活动的导航抽屉和工具栏

时间:2021-03-23 22:32:30

标签: android xml android-layout navigation-drawer toolbar

我想编写一个带有导航抽屉的工具栏,它会出现在我的应用程序的所有活动中。为此,我创建了一个抽象类“BaseActivity”,然后由所有其他活动对其进行扩展。

我已经成功地创建了一个出现在所有活动中的工具栏。这是看起来像的屏幕截图:

Toolbar across all activities

但是,当我尝试向该工具栏添加导航抽屉时,扩展 BaseActivity.java 的活动的内容不会出现。

Activity not visible under Toolbar and Navigation Drawer

因此,我想知道我在执行导航抽屉时做错了什么。这是activity_base.xml的布局

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:title="@string/app_name"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <com.google.android.material.navigation.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:menu="@menu/nav_menu"
            android:id="@+id/nav_view"/>
    </androidx.drawerlayout.widget.DrawerLayout>

</merge>

这里是“BaseActivity.java”的java代码:

package com.example.newsreader;

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.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

public abstract class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected boolean useToolbar() {
        return true;
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        configureToolbar(view);
        super.setContentView(view);

        //create NavigationDrawer:
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open, R.string.closed);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

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

    private void configureToolbar(View view)  {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        if (toolbar != null)  {
            if (useToolbar())  {
                //loads toolbar and calls onCreateOptionsMenu
                setSupportActionBar(toolbar);


            } else  {
                toolbar.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)  {
        //inflate menu items for use in action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String message = null;
        //Look at your menu XML file. Put a case for every id in that file:
        switch(item.getItemId())
        {
            //what to do when the menu item is selected:
            case R.id.item1:
                message = "You clicked on item 1";
                break;
            case R.id.item2:
                message = "You clicked on item 2";
                break;
            case R.id.item3:
                message = "You clicked on item 3";
                break;
            case R.id.item4:
                message = "You clicked on the overflow menu";
                break;
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
        return true;
    }

    // Needed for the OnNavigationItemSelected interface:
    @Override
    public boolean onNavigationItemSelected( MenuItem item) {

        String message = null;

        switch(item.getItemId())
        {
            case R.id.item1:
                Intent chatPage = new Intent(this, NewsHeadlines.class);
                startActivity(chatPage);
                break;
            case R.id.item2:
                Intent weatherForecastPage = new Intent(this, WeatherForecast.class);
                startActivity(weatherForecastPage);
                break;
            case R.id.item3:
                setResult(500);
                finish();
                break;
        }

        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        drawerLayout.closeDrawer(GravityCompat.START);

        return false;
    }
}

对我做错了什么有什么建议吗?

1 个答案:

答案 0 :(得分:0)

当我们在应用中使用导航栏或底部导航栏时,强烈建议使用 Fragments 而不是 Activity

由于您在 onNavigationItemSelected 方法中使用了 Activity,这是错误的:

Intent chatPage = new Intent(this, NewsHeadlines.class); startActivity(chatPage);

相反,使用这样的 Fragment > getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new **yourFragmentName**()).commit();