如何更改动作条菜单项的图标编程 [英] How to change MenuItem icon in ActionBar programatically
问题描述
如何更改动作条菜单项的图标编程?我试图用
菜单项菜单项=(菜单项)findViewById(R.id.action_settings); menuItem.setIcon(getResources()。getDrawable(R.drawable.ic_launcher))
,但它不工作。 这是我的code:
MainActivity
com.test包;
进口android.os.Bundle;
进口android.support.v7.app.ActionBarActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;
公共类MainActivity扩展ActionBarActivity {
私人Button按钮;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮=(按钮)findViewById(R.id.button);
button.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
菜单项菜单项=(菜单项)findViewById(R.id.action_settings);
menuItem.setIcon(getResources()getDrawable(R.drawable.ic_launcher));
}
});
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
main.xml中
<菜单
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
工具:上下文=MainActivity。>
<项目
机器人:图标=@可绘制/ action_settings
机器人:ID =@ + ID / action_settings
机器人:标题=@字符串/ action_settings
机器人:orderInCategory =100
应用程序:showAsAction =总是/>
< /菜单>
activity_main
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT>
<按钮
机器人:ID =@ + ID /按钮
机器人:文本=设置图标
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
这是我跑后有过的异常:
菜单项菜单项=(菜单项)findViewById(R.id.action_settings);
11-09 19:52:40.471 1735年至1735年/ com.test E / AndroidRuntime:致命异常:主要
java.lang.ClassCastException:android.support.v7.internal.view.menu.ActionMenuItemView
在com.test.MainActivity $ 1.onClick(MainActivity.java:19)
在android.view.View.performClick(View.java:2485)
在android.view.View $ PerformClick.run(View.java:9080)
在android.os.Handler.handleCallback(Handler.java:587)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:3683)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:507)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
在dalvik.system.NativeStart.main(本机方法)
在的onCreate菜单项不能使用findViewById(),因为菜单布局不膨胀。您可以创建一个全局菜单变量并初始化它在onCreateOptionsMenu(),然后用它在你的onClick()。
私人菜单菜单;
在你的onCreateOptionsMenu()
this.menu =菜单;
在您的按钮的onClick()方法
menu.getItem(0).setIcon(getResources()getDrawable(R.drawable.ic_launcher)。);
How to change MenuItem icon in ActionBar programatically? 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
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
<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)
You can't use findViewById() on menu items in onCreate because the menu layout isn't inflated. You could create a global Menu variable and initialize it in the onCreateOptionsMenu() and then use it in your onClick().
private Menu menu;
In your onCreateOptionsMenu()
this.menu = menu;
In your button's onClick() method
menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_launcher));
这篇关于如何更改动作条菜单项的图标编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!