温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - why custom menu icon is not responding
android-custom-view

其他 - 为什么自定义菜单图标没有响应

发布于 2020-03-27 16:05:11

我想在具有图像视图的工具栏中添加注销选项。由于工具栏菜单中的尺寸固定,我通过指定去了自定义菜单

app:actionLayout =“ @ layout / custom_menu”

我在中指定了此项,并在custom_menu.xml中做了一个自定义菜单。

现在,当我单击该项目时,我想执行一些任务,让我们调用诸如吐司消息之类的东西进行测试。

但是,当我单击工具栏中的时,没有任何反应。

我在这里做错了什么?

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>

查看更多

查看更多

提问者
selvabharathi s
被浏览
17
selvabharathi s 2020-02-01 01:25

实际上,我们的自定义菜单项不会自动获得与普通菜单项相同的填充。因此,可以大大减少接收触摸事件的区域。我只是在四处寻找以单击正确的区域。我们可以通过在自定义视图中添加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>

这是结果。

在此处输入图片说明