您如何在所有活动中使用工具栏

How do you use a toolbar across all activies

我制作了一个工具栏和导航抽屉 java class 及其 XML,它工作得很好。 这是代码:

Java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar1);
    setSupportActionBar(toolbar);

    //This part is new for NavigationDrawer:
    DrawerLayout 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();

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

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.items, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.ticket:
            startActivity(new Intent(NavigationActivity.this, TicketMasterActivity.class));
            break;

        case R.id.recipe:
            Intent goToRecipe = new Intent(NavigationActivity.this, RecipeSearchActivity.class);
            startActivity(goToRecipe);
            break;

        case R.id.covid:
            Intent goToCovid = new Intent(NavigationActivity.this, MainActivity_covid.class);
            startActivity(goToCovid);
            break;

        case R.id.album:
            Intent goToAlbum = new Intent(NavigationActivity.this, MainActivity.class);
            startActivity(goToAlbum);
            break;

        case R.id.help:
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle(R.string.WelcomeTotal);
            alertDialogBuilder.setMessage(R.string.WelcomeTotalMessage);
            alertDialogBuilder.setPositiveButton("OK", (click2, arg) -> {
            });
            alertDialogBuilder.create().show();
    }
    return true;
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.ticket1) {
        startActivity(new Intent(NavigationActivity.this, TicketMasterActivity.class));
    } else if (id == R.id.recipe1) {
        Intent goToRecipe = new Intent(NavigationActivity.this, RecipeSearchActivity.class);
        startActivity(goToRecipe);
    } else if (id == R.id.covid1) {
        Intent goToCovid = new Intent(NavigationActivity.this, MainActivity_covid.class);
        startActivity(goToCovid);

    } else if (id == R.id.album1) {
        Intent goToAlbum = new Intent(NavigationActivity.this, MainActivity.class);
        startActivity(goToAlbum);
    } else if (id == R.id.share) {
        Toast.makeText(this, "this feature is not yet implemented", Toast.LENGTH_LONG).show();
    } else if (id == R.id.send) {
        Toast.makeText(this, "this feature is not yet implemented", Toast.LENGTH_LONG).show();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

XML:

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <androidx.appcompat.widget.Toolbar
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:id="@+id/toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"/>
    
    <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:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:itemTextColor="#000000"
            app:headerLayout="@layout/naviheader"
            app:menu="@menu/navimenu"
            tools:ignore="MissingClass" />
    </androidx.drawerlayout.widget.DrawerLayout>
</LinearLayout>

我想让每个 activity 都使用这个特定的工具栏。而且每个工具栏都预加载了我放在 java class 中的项目。这可能吗?如果可能,怎么做?

谢谢。

创建并Activity,它将作为具有您自己的自定义工具栏的父级 Activity。 将 activity 扩展到任何你想要的地方。