如何以编程方式更改ActionBar中的MenuItem图标 [英] How to change MenuItem icon in ActionBar programmatically
问题描述
如何以编程方式更改ActionBar中的MenuItem图标?我尝试使用
How to change MenuItem icon in ActionBar programmatically? I tried to use
MenuItem menuItem = (MenuItem)findViewById(R.id.action_settings);
menuItem.setIcon(getResources().getDrawable(R.drawable.ic_launcher))
但是它不起作用. 这是我的代码:
but it doesn't work. This is my code:
MainActivity
MainActivity
package com.test;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MenuItem menuItem = (MenuItem)findViewById(R.id.action_settings);
menuItem.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
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=".MainActivity" >
<item
android:icon="@drawable/action_settings"
android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
activity_main
activity_main
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button
android:id="@+id/button"
android:text="Set icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这是我跑步后遇到的异常:
This is the exception that i've had after running:
MenuItem menuItem = (MenuItem)findViewById(R.id.action_settings);
11-09 19:52:40.471 1735-1735/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.support.v7.internal.view.menu.ActionMenuItemView
at com.test.MainActivity$1.onClick(MainActivity.java:19)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
推荐答案
您不能在onCreate()中的菜单项上使用findViewById(),因为菜单布局尚未膨胀.您可以创建一个全局Menu变量,然后在onCreateOptionsMenu()中对其进行初始化,然后在onClick()中使用它.
You can't use findViewById() on menu items in onCreate() because the menu layout isn't inflated yet. You could create a global Menu variable and initialize it in the onCreateOptionsMenu() and then use it in your onClick().
private Menu menu;
在您的onCreateOptionsMenu()
In your onCreateOptionsMenu()
this.menu = menu;
在按钮的onClick()方法中
In your button's onClick() method
menu.getItem(0).setIcon(ContextCompat.getDrawable(this, R.drawable.ic_launcher));
这篇关于如何以编程方式更改ActionBar中的MenuItem图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!