如何在android studio中切换应用程序栏上的菜单图标
Erm不使用切换按钮,我可以在应用程序栏中切换图标吗 就像当我在应用程序栏中按下我的图标时,它会变为其他图标 这是我的菜单_main.xml如何在android studio中切换应用程序栏上的菜单图标,android,android-studio,material-design,Android,Android Studio,Material Design,Erm不使用切换按钮,我可以在应用程序栏中切换图标吗 就像当我在应用程序栏中按下我的图标时,它会变为其他图标 这是我的菜单_main.xml <menu 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"
<menu 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"
tools:context="course.examples.healthcare_application.MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="1"
android:title="@string/action_settings"
app:showAsAction="never" />
<!--lower value will be on the left-->
<item
android:id="@+id/Bluetooth_connect"
android:icon="@drawable/ic_bluetooth_white_24dp"
app:showAsAction="always"
android:orderInCategory="2"
android:title="@string/bluetooth_connect" />
</menu>
要实现切换效果,可以在单击按钮时更改按钮的颜色。如果图标是可绘制的,则可以使用“着色”属性设置颜色 在xml中,可以使用
android:tint="@color/icon_pressed"
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
在代码中,可以使用
android:tint="@color/icon_pressed"
imageView.setColorFilter(Color.argb(255, 255, 255, 255));
在可绘制文件夹中的选择器中定义它们,如下所示: button_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/pressed" />
<item android:state_pressed="true" android:drawable="@drawable/initialpressed"/>
<item android:state_checked="true" android:drawable="@drawable/on"/>
<item android:drawable="@drawable/initial"/>
</selector>
你好呃,我需要在可绘图文件夹中创建一个新文件吗?我不太明白你的意思。事实上,我没有使用按钮。我正在使用菜单文件夹中的图标好的。你只需尝试一个简单的教程,我想要的是应用程序栏上的图标,而不是WindowBackground上的图标。请给我看看你的onclicklistenerHi。我上传了主要的活动代码。我没有使用onclicker。我正在使用onOptionMenuselected。@2查看如何取消阻止此操作?为什么它被搁置?