如何在自定义Toolbar中添加溢出菜单?已配置却不显示的解决方法
自定义Toolbar不显示溢出菜单的解决方法
问题描述
我想要使用自定义Toolbar来设置标题和副标题,但同时也想添加溢出菜单。我已经完成相关配置,但Toolbar仍不显示溢出菜单,请问我该如何解决?
相关代码
主Activity布局xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BluetoothMain" android:orientation="vertical" tools:menu="@menu/bluetooth_overflow_menu"> <include layout="@layout/bluetooth_toolbar" android:visibility="visible"/> </LinearLayout>
自定义Toolbar布局
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar 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:id="@+id/BluetoothToolBar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/yellowGreenColorWheel" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:title="Bluetooth" app:subtitle="Not Connected" tools:menu="@menu/bluetooth_overflow_menu"> </androidx.appcompat.widget.Toolbar>
溢出菜单xml(添加了蓝牙图标和设置选项)
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_favorite" android:icon="@drawable/bluetooth_white_icon" android:title="action_favorite" app:showAsAction="ifRoom"/> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_settings" android:title="Settings" app:showAsAction="never"/> </menu>
Build Gradle文件(minSdk=28,targetSdk=32)
android { compileSdk 32 defaultConfig { applicationId "com.indiebloog.IndieNode" minSdk 28 targetSdk 32 versionCode 1 versionName "1.0" testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } namespace 'com.indiebloog.IndieNode' }
解决步骤
你当前配置里的tools:menu仅用于Android Studio预览,不会在运行时加载菜单,必须通过Activity代码手动关联并加载菜单:
将Toolbar设置为ActionBar
在BluetoothMain的onCreate方法中,获取Toolbar实例并设置为应用的ActionBar:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_main_activity_layout); // 替换为你的主布局ID Toolbar toolbar = findViewById(R.id.BluetoothToolBar); setSupportActionBar(toolbar); }重写方法加载菜单
在Activity中添加以下方法,加载溢出菜单资源:@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.bluetooth_overflow_menu, menu); return true; }(可选)处理菜单点击事件
若需要响应菜单点击,重写onOptionsItemSelected:@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { // 编写设置菜单的点击逻辑 return true; } else if (id == R.id.action_favorite) { // 编写蓝牙图标菜单的点击逻辑 return true; } return super.onOptionsItemSelected(item); }检查主题配置
确保App主题继承自无默认ActionBar的AppCompat主题,比如Theme.AppCompat.Light.NoActionBar,避免系统默认ActionBar覆盖自定义Toolbar:<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 其他主题配置 --> </style>
完成以上步骤后,运行应用即可看到Toolbar上的溢出菜单。
内容的提问来源于stack exchange,提问作者indiebloog
相关产品推荐
相关产品推荐

