这是我的activity_main.xml
,右边有一个基本的Toolbar
和两个Button
(还有一个NavigationView
):
<?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:background="@drawable/mxhbg"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="com.myapp.MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
android:orientation="vertical"
android:keepScreenOn="true"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="horizontal">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<Button
android:id="@+id/btnRandom"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@drawable/ic_shuffle">
</Button>
<Button
android:id="@+id/btnRescanSongs"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@android:drawable/stat_notify_sync">
</Button>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
// other stuff
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/nav_view"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
我在MainActivity上这样设置:
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
现在,在另一个Activity(SyncActivity)上,我想显示相同的Toolbar
,但右边有不同的图标.
正确的方法是什么?
在activity_sync.xml
秒内执行此操作:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@drawable/mxhbg"
tools:context=".SyncActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/mxhbg"
android:orientation="vertical"
android:keepScreenOn="true"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="horizontal">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<Button
android:id="@+id/btnRescanSongs"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:background="@android:drawable/stat_notify_sync">
</Button>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<ListView
android:id="@+id/log"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"
android:padding="8dp">
</ListView>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在SyncActivity上:
setContentView(R.layout.activity_sync);
Objects.requireNonNull(getSupportActionBar()).setTitle("Sync");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
它在主条目的下面增加了一个新的Toolbar
个.
另外:为什么主Toolbar
上的图标不显示在其他Activity上?似乎隐藏了他们?为什么?