Erm而不是使用切换按钮,我可以切换应用程序栏中的图标吗?
就像当我在应用程序栏中按我的图标时,它将更改为其他图标
这是我的menu_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"
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>
主要活动代码
@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.Bluetooth_connect){
Toast.makeText(getApplicationContext(), "Turned on", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
您可以在可绘制文件夹中的选择器中定义它们:
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>
然后在您的按钮中定义它,如下所示:
android:background="@drawable/button_state"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句