android-custom-view - 为什么自定义菜单图标没有响应
问题描述
我想在具有图像视图的工具栏中添加一个注销选项。由于工具栏菜单中的固定大小,我通过指定自定义菜单
app:actionLayout="@layout/custom_menu"
我在我的 中指定了这个,并在 custom_menu.xml 中做了一个自定义菜单。
现在,当我单击该项目时,我想执行一些任务,让我们调用类似 toast 消息的内容进行测试。
但是当我单击工具栏中的 时,没有任何反应。
我在这里做错了什么?
MyJavaActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logout, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.logOut) {
Toast.makeText(UserActivity.this, "logout clicked", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
logout.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/logOut"
android:title="logout"
app:actionLayout="@layout/custom_menu"
app:showAsAction="ifRoom">
</item>
</menu>
custom_menu.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="wrap_content"
android:clickable="true"
android:layout_gravity="center">
<ImageView
android:id="@+id/logout"
android:layout_width="@dimen/menu_item_icon_size"
android:layout_height="@dimen/menu_item_icon_size"
android:layout_gravity="center"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_logout"
android:clickable="true"
android:elevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
实际上,我们的自定义菜单项不会自动获得与普通菜单项相同的填充。所以接收触摸事件的区域大大减少了。我只是四处寻找点击正确的区域。我们可以通过在自定义视图中添加 FrameLayout 来解决这个问题。
确保您的 FrameLayout 高度和宽度大于自定义图标的高度和宽度。
这是我在 custom_menu.xml 中所做的更改
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="@dimen/menu_item_size"
android:layout_height="@dimen/menu_item_size">
<FrameLayout
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/logout"
android:layout_width="@dimen/menu_item_icon_size"
android:layout_height="@dimen/menu_item_icon_size"
android:layout_gravity="center"
android:elevation="5dp"
android:src="@drawable/ic_logout" />
</FrameLayout>
</FrameLayout>
这是结果。
推荐阅读
- php - php读取文件夹中的.txt文件,然后计算换行符
- python - ImportError: libcublas.so.9.0 for tensorflow in jupyter notebook
- typescript - 是否有一种自动、更高效、更快捷的方式来导出所有模块?
- c++11 - pop_front() 出现错误“只读对象中的成员分配”
- reactjs - 只要在我的 React JS 应用程序中调用任何函数,Youtube 视频就会自动停止
- cakephp-3.0 - 将 CakePHP 版本从 3.6 升级到 3.7 时出现错误 App\\Application
- ocaml - Functor 没有正确继承签名
- c# - 我们如何将平面文件中的一行一行插入到 OLEdb 目标数据库中?
- mysql - 两列上的 SQL groupby
- html - onmouseover/out 的初始图像不会加载,但在第一次鼠标悬停后会正常工作