[英]Android ToolBar Menu Item not Clickable
My android app menu item is not clickable I Add app:showAsAction="always"
and android:enabled="true"
to the menu item and The Item Button is shown but it's not clickable, What Im missing here?我的 android 应用程序菜单项不可点击我将
app:showAsAction="always"
和android:enabled="true"
添加到菜单项,并且显示了项目按钮但它不可点击,这里缺少什么?
App Theme: parent="Theme.MaterialComponents.DayNight.DarkActionBar"应用主题:parent="Theme.MaterialComponents.DayNight.DarkActionBar"
Layout:布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:orientation="vertical"
tools:context=".Settings.ReportTable">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:id="@+id/toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/balance_table_SV"/>
</LinearLayout>
Activity:活动:
public class ReportTable extends AppCompatActivity {
ScrollView balance_table_SV;
TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_table);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.excel);
balance_table_SV = findViewById(R.id.balance_table_SV);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.excel, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.menu.excel) {
export_to_excel();
}
return super.onOptionsItemSelected(item);
}
}
First of all, it looks like you're comparing the item ID to the resource ID of the entire menu ( R.menu.excel
).首先,看起来您正在将项目 ID 与整个菜单的资源 ID (
R.menu.excel
) 进行比较。 You should have an ID for each menu item to use for click handling:每个菜单项都应该有一个 ID 用于点击处理:
<item android:id="@+id/excel" />
if(item.getItemId() == R.id.excel) { ... }
Secondly there seems to be quite a bit of confusion here between the Toolbar
and the Android action bar.其次,
Toolbar
和 Android 操作栏之间似乎有些混淆。 You are you using a mixture of both in the code you have provided which is causing the inconsistency.您是否在提供的代码中混合使用了两者,这导致了不一致。
Firstly, if you're using a Toolbar
in your layout, you don't need the action bar at all, therefore you should be inheriting from a NoActionBar
Material Components theme (such as Theme.MaterialComponents.DayNight.NoActionBar
).首先,如果您在布局中使用
Toolbar
,则根本不需要操作栏,因此您应该从NoActionBar
材质组件主题(例如Theme.MaterialComponents.DayNight.NoActionBar
)继承。 Then in order to setup your Toolbar
you have two options:然后,为了设置您的
Toolbar
,您有两个选项:
Toolbar
as the action barToolbar
设置为操作栏With this approach you can register the Toolbar
to act as the action bar for an activity, which means it correctly triggers the onOptionsItemSelected
callback etc. So to do this we need to register it in onCreate
:使用这种方法,您可以将
Toolbar
注册为活动的操作栏,这意味着它可以正确触发onOptionsItemSelected
回调等。因此,我们需要在onCreate
中注册它:
setSupportActionBar(toolbar);
Then you should probably be using onCreateOptionsMenu
to inflate the menu instead of Toolbar.inflateMenu
just as you've already done in your code:然后你可能应该使用
onCreateOptionsMenu
来膨胀菜单而不是Toolbar.inflateMenu
就像你已经在你的代码中所做的那样:
getMenuInflater().inflate(R.menu.excel, menu);
And finally to listen for menu item clicks, you can override onOptionsItemSelected
and check the item ID as I outlined above.最后,要监听菜单项点击,您可以覆盖
onOptionsItemSelected
并检查项目 ID,如上所述。
Toolbar
independentlyToolbar
Instead of registering the Toolbar
as the action bar, you can instead just use the Toolbar
like any regular view and set it up directly.无需将
Toolbar
注册为操作栏,您可以像任何常规视图一样使用Toolbar
并直接进行设置。 Firstly to inflate the menu you can call:首先要膨胀菜单,您可以调用:
toolbar.inflateMenu(R.menu.excel);
Then in order to listen for click events you can attach an OnMenuItemClickListener
as follows:然后为了监听点击事件,你可以附加一个
OnMenuItemClickListener
如下:
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
...
}
});
Both approaches are valid but I personally prefer the second.两种方法都有效,但我个人更喜欢第二种。 It seems strange to try and work around the activity lifecycle when you could just treat the
Toolbar
normally.当您可以正常处理
Toolbar
时,尝试绕过活动生命周期似乎很奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.