Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

android - Custom PopupMenu (layout)

I'm trying to upgrade my PopupMenu so it would come with icons and custom styles.
I have created a new layout for it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/layout_sharea"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetexta"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
     <RelativeLayout
        android:id="@+id/layout_shareb"
        android:background="@drawable/share"
        android:paddingLeft="10.0dip"
        android:paddingRight="10.0dip"
        android:layout_width="wrap_content"
        android:layout_height="50.0dip"
        android:layout_below="@+id/layout_sharea"
        android:onClick="share">
        <TextView
            android:id="@+id/sharetextb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/share"
            android:drawableLeft="@drawable/share_button"
            android:drawablePadding="10.0dip"
            android:layout_centerVertical="true" />
     </RelativeLayout>
</RelativeLayout>

I want the PopupMenu to be customized (to be this layout) like in this picture PopupMenu

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A PopupMenu is meant for displaying Menus and there really isn't a good way of customizing the appearance of the menu items. If you want something more flexible, your answer is ListPopupWindow.

private static final String TITLE = "title";
private static final String ICON = "icon";

private List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

// Use this to add items to the list that the ListPopupWindow will use
private void addItem(String title, int iconResourceId) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put(TITLE, title);
    map.put(ICON, iconResourceId);
    data.add(map);
}

// Call this when you want to show the ListPopupWindow
private void showListMenu(View anchor) {
    ListPopupWindow popupWindow = new ListPopupWindow(this);

    ListAdapter adapter = new SimpleAdapter(
            this,
            data,
            android.R.layout.activity_list_item, // You may want to use your own cool layout
            new String[] {TITLE, ICON}, // These are just the keys that the data uses
            new int[] {android.R.id.text1, android.R.id.icon}); // The view ids to map the data to


    popupWindow.setAnchorView(anchor);
    popupWindow.setAdapter(adapter);
    popupWindow.setWidth(400); // note: don't use pixels, use a dimen resource
    popupWindow.setOnItemClickListener(myListener); // the callback for when a list item is selected
    popupWindow.show();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...