2017-07-04 83 views

回答

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(); 
相關問題