为什么自定义菜单图标没有响应
why custom menu icon is not responding
我想在有图像视图的工具栏上添加注销选项。由于工具栏菜单中的固定大小,我通过指定
选择了自定义菜单
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>
这是结果。
我想在有图像视图的工具栏上添加注销选项。由于工具栏菜单中的固定大小,我通过指定
选择了自定义菜单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>
这是结果。