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

yshpjwxd  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(353)

我制作了一个工具栏和导航抽屉java类以及它的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>

我想让每个活动都使用这个特定的工具栏。每个工具栏都预装了我放在java类中的项目。这可能吗?如果可能,怎么可能?
谢谢您。

66bbxpm5

66bbxpm51#

创建一个活动,该活动将使用您自己的自定义工具栏作为父活动。你想去哪里就去哪里。

相关问题