0
A
回答
1
LinearLayout menuLayout= (LinearLayout) activity.getLayoutInflater().inflate(menuId, null);
int layoutCount = menuLayout.getChildCount();
for (int i = 0; i < layoutCount; i++)
{
View itemView = menuLayout.getChildAt(i);
if (itemView instanceof LinearLayout)
{
LinearLayout itemLayout = (LinearLayout) itemView;
int count = ((LinearLayout) itemView).getChildCount();
for (int j = 0; j < count; j++)
{
View view = itemLayout.getChildAt(j);
if (view instanceof ImageView)
((ImageView) view).setImageResource(R.drawable.newImage);
}
}
}
您可以使用findViewById查找项目。
0
You can change the background by using
LinearLayout ll=(LinearLayout) findViewById(R.id.linear);
ll.setBackground(...);
ImageView i=popUpwindow.findViewById(R.id.image1);
i.setImageResource(id here);
or
i.setImageDrawable(Drawable here);
+0
ActivityMain.A.displayPopupWindow(ActivityAllApps.this,视图,R.layout.popup_menu_all_apps_app) ; 需要以编程方式将绿色图标替换为其他图标,这些图标也将位于文件夹Drawable中 – GAAAN
0
首先初始化要改变ImageView的。就像这样:
ImageView imageView = (ImageView) findViewById(R.id.yourImageViewId);
然后
imageView.setImageResource(R.drawable.yournewIconId);
同样可以为背景变化做到这一点。
0
您需要申请一个主题风格如下:
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@color/colorPrimary</item>
<item name="android:background">@color/colorPrimary</item>
<item name="android:textColorPrimary">@android:color/darker_gray</item>
</style>
下面的代码工作对我来说:对于弹出式菜单
public void showPopUpMenu(final Context context, View view) {
Context wrapper = new ContextThemeWrapper(context, R.style.PopupMenu);
PopupMenu popup = new PopupMenu(wrapper, view, Gravity.END);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_options, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_share:
shareWithFriends();
break;
default:
break;
}
return true;
}
});
try {
Field[] fields = popup.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
Class<?> classPopupHelper = Class.forName(menuPopupHelper
.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod(
"setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
popup.show();
}
0
XML布局在菜单通常创建文件夹
您必须在调用popup.show()之前对其进行更改()
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.name_of_xml_layout, popup.getMenu());
popup.getMenu().findItem(R.id.editmenubtn).setIcon(R.drawable.newicon); //this line will change the icon of popup menu
popup.show();
相关问题
- 1. 如何以编程方式更改css背景图片?
- 2. 如何以编程方式更改按钮的背景颜色
- 3. Avalonedit如何以编程方式更改文本的背景
- 4. 如何以编程方式更改对话框背景颜色?
- 5. 如何以编程方式更改列表项目的背景?
- 6. 以编程方式更改UIButton上的背景图像
- 7. UIBarButtonItem - 以编程方式更改背景图像
- 8. Silverlight/WP7:以编程方式更改按钮背景图像
- 9. UIButton背景图像以编程方式更改
- 10. 以编程方式更改背景颜色的视图
- 11. 通过编程方式更改背景
- 12. 以编程方式更改MediaController图标
- 13. 以编程方式更改ActionBar图标
- 14. 以编程方式更改所需的背景模式
- 15. 如何以编程方式更改窗体c上的背景图像#
- 16. 以编程方式更改椭圆形按钮背景
- 17. oracle窗体以编程方式更改窗口背景颜色?
- 18. XNA以编程方式更改背景颜色
- 19. 以编程方式更改桌面背景
- 20. 以编程方式更改某些文本的背景颜色
- 21. 以编程方式更改列表框项目背景颜色
- 22. 以编程方式更改ListView行的背景颜色(wpf)
- 23. 以编程方式更改条形图按钮背景的图像大小?
- 24. 以编程方式在标签的背景中设置图像
- 25. 如何以编程方式重复工具栏背景图像?
- 26. iPhone:如何以编程方式删除按钮背景图片?
- 27. 如何以编程方式检测图像的背景?
- 28. 如何设置图像作为背景以编程方式,iphone
- 29. 如何以编程方式更改.bat文件的图标?
- 30. 如何以编程方式更改按钮的图标?
发表您的XML代码 – vm345
xml文件https://drive.google.com/file/d/0Bxhi0uFKK3upNWhJOV9oX2sxUEk/view?usp=sharing – GAAAN