2014-10-10 112 views
2

我試圖升級我的PopupMenu,所以它會帶有圖標和自定義樣式。
我已經創建了一個新的佈局也自定義PopupMenu(佈局)

<?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> 

我想要的彈出菜單在這個畫面進行定製(是這個佈局)像 PopupMenu

+0

引用了一個簡單的自定義文字顏色/背景修復這個【答案】(https://stackoverflow.com/a/40017199/4625829)。 – 2017-12-01 08:07:23

回答

21

一個PopupMenu是爲顯示Menus和真的不是定製菜單項外觀的好方法。如果你想要更靈活的東西,你的答案是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(); 
} 
+0

這是我第一次嘗試這樣做,你能告訴我一個我能做到的例子嗎? – 2014-10-10 19:39:00

+0

我使用彈出窗口沒有列表菜單(自定義佈局) 現在我有一個定位它的問題,X座標不起作用。 你可以檢查嗎? http://stackoverflow.com/questions/26308837/popup-window-stays-on-left – 2014-10-11 07:34:24

1

下面是自定義適配器

Model

public class Item { 
    private String title; 
    private int imageRes; 

    public Item(String title, int imageRes) { 
     this.title = title; 
     this.imageRes = imageRes; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public int getImageRes() { 
     return imageRes; 
    } 

    public void setImageRes(int imageRes) { 
     this.imageRes = imageRes; 
    } 
} 

ListAdapter

public class ListPopupWindowAdapter extends BaseAdapter { 
    LayoutInflater mLayoutInflater; 
    List<Item> mItemList; 

    public ListPopupWindowAdapter(Context context, List<Item> itemList) { 
     mLayoutInflater = LayoutInflater.from(context); 
     mItemList = itemList; 
    } 

    @Override 
    public int getCount() { 
     return mItemList.size(); 
    } 

    @Override 
    public Item getItem(int i) { 
     return mItemList.get(i); 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mLayoutInflater.inflate(R.layout.item, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.tvTitle.setText(getItem(position).getTitle()); 
     holder.ivImage.setImageResource(getItem(position).getImageRes()); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView tvTitle; 
     ImageView ivImage; 

     ViewHolder(View view) { 
      tvTitle = view.findViewById(R.id.text); 
      ivImage = view.findViewById(R.id.image); 
     } 
    } 
} 

item.xml

012我的自定義 ListPopupWindow演示
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

最後,顯示ListPopupWindow

private void showListPopupWindow(View anchor) { 
    final ListPopupWindow popupWindow = new ListPopupWindow(this); 

    List<Item> itemList = new ArrayList<>(); 
    itemList.add(new Item("A", R.mipmap.ic_launcher)); 
    itemList.add(new Item("B", R.mipmap.ic_launcher)); 
    itemList.add(new Item("C", R.mipmap.ic_launcher)); 

    ListAdapter adapter = new ListPopupWindowAdapter(this, itemList); 
    popupWindow.setAnchorView(anchor); 
    popupWindow.setAdapter(adapter); 
    popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      popupWindow.dismiss(); 
     } 
    }); 
    popupWindow.show(); 
}