PopupWindow如何以编程方式更改图标和背景?

时间:2017-07-04 10:07:44

标签: java android

拥有PopupWindow的xml文件:

image

如何以编程方式替换此xml中的图标和Backgroud?

5 个答案:

答案 0 :(得分:1)

弹出菜单的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 :(得分: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找到项目。

答案 2 :(得分:0)

&#13;
&#13;
You can change the background by using 
  LinearLayout ll=(LinearLayout) findViewById(R.id.linear);
ll.setBackground(...);
&#13;
&#13;
&#13;

&#13;
&#13;
ImageView i=popUpwindow.findViewById(R.id.image1);

i.setImageResource(id here);
or 
i.setImageDrawable(Drawable here);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

首先初始化要更改的imageView。像这样:

ImageView imageView = (ImageView) findViewById(R.id.yourImageViewId);

然后

imageView.setImageResource(R.drawable.yournewIconId);

同样,你可以为背景变化做到这一点。

答案 4 :(得分: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();
        }