Have ist XML-Datei für PopupWindow:PopupWindow Wie programmgesteuert Symbol und Hintergrund ändern?
Wie programmatisch auf das Symbol und Backgroud in diesem xml ersetzen?
Have ist XML-Datei für PopupWindow:PopupWindow Wie programmgesteuert Symbol und Hintergrund ändern?
Wie programmatisch auf das Symbol und Backgroud in diesem xml ersetzen?
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);
}
}
}
Sie können Elemente mit findViewById finden.
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);
ActivityMain.A.displayPopupWindow (ActivityAllApps.this, Ansicht, R.layout.popup_menu_all_apps_app) ; Programmatisch erforderlich, um grüne Symbole zu anderen Symbolen zu ersetzen, die sich auch im Ordner Drawable befinden – GAAAN
Zuerst initialisieren die Imageview Sie ändern möchten. Wie folgt aus:
ImageView imageView = (ImageView) findViewById(R.id.yourImageViewId);
dann
imageView.setImageResource(R.drawable.yournewIconId);
Ebenso können Sie es für den Hintergrund ändern tun.
Sie benötigen ein Thema Art, wie folgend anwenden:
<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>
folgenden Code funktioniert für mich:
Layoutpublic 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();
}
XML für Popup-Menü wird in der Regel in einem Menü erstellt Ordner
Sie müssen es ändern, bevor Sie das popup.show()
aufrufenMenuInflater 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();
posten Sie Ihre XML-Code – vm345
XML-Datei https://drive.google.com/file/d/0Bxhi0uFKK3upNWhJOV9oX2sxUEk/view?usp=sharing – GAAAN