这是我的
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
上的图标没有显示在其他活动上?看来是把他们藏起来了?为什么?
您可以从布局 xml 中删除所有工具栏按钮并将它们移动到菜单 xml 中。
文件:
res/menu/yourmenuname.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_rescan_songs"
android:icon="@android:drawable/stat_notify_sync"
android:title="@string/menu_rescan_songs"
app:showAsAction="always"/>
</menu>
请注意,您还必须提供标题,而不仅仅是图标,您可以使用 showAsAction 与
always
来始终显示图标,或者 ifRoom
或 ifRoom|withText
只是尝试您最喜欢的内容。
然后你覆盖这个函数并加载你的菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourmenuname,menu);
return super.onCreateOptionsMenu(menu);
}
要处理菜单操作,请使用:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_rescan_songs:
doSomething();
return true;
// case R.id.action_settings: // in case you need an extra button on
// startSettings(); // on this activity
// return true;
default:
return super.onOptionsItemSelected(item);
}
}
您可以使用菜单做很多事情,在这里添加一些链接可以帮助您继续阅读它们: