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を(このレイアウトに)カスタマイズしたい
PopupMen は 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();
}
これがカスタムアダプタによるカスタムListPopupWindow
のデモです
Model
public class ListPopupItem {
private String title;
private int imageRes;
public ListPopupItem(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;
}
}
ListAdapter
public class ListPopupWindowAdapter extends BaseAdapter {
private List<ListPopupItem> items;
public ListPopupWindowAdapter(List<ListPopupItem> items) {
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public ListPopupItem getItem(int i) {
return items.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 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_popup, 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_list_popup.xml
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal"
>
<ImageView
Android:id="@+id/image"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
tools:ignore="ContentDescription"
tools:src="@mipmap/ic_launcher"
/>
<TextView
Android:id="@+id/text"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center_vertical"
tools:text="Title"
/>
</LinearLayout>
最後に、show ListPopupWindow
(In this demo, I show MATCH_PARENT popup here, you can show a specific with for popup (eg: 100px, 200px,...) If you set popup width = WRAP_CONTENT, popup width will equals ANCHOR width)
private void showListPopupWindow(View anchor) {
List<ListPopupItem> listPopupItems = new ArrayList<>();
listPopupItems.add(new ListPopupItem("Menu 1", R.mipmap.ic_launcher));
listPopupItems.add(new ListPopupItem("Menu 2", R.mipmap.ic_launcher));
listPopupItems.add(new ListPopupItem("Menu 3", R.mipmap.ic_launcher));
final ListPopupWindow listPopupWindow =
createListPopupWindow(anchor, ViewGroup.LayoutParams.MATCH_PARENT, listPopupItems);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listPopupWindow.dismiss();
Toast.makeText(MainActivity.this, "clicked at " + position, Toast.LENGTH_SHORT)
.show();
}
});
listPopupWindow.show();
}
private ListPopupWindow createListPopupWindow(View anchor, int width,
List<ListPopupItem> items) {
final ListPopupWindow popup = new ListPopupWindow(this);
ListAdapter adapter = new ListPopupWindowAdapter(items);
popup.setAnchorView(anchor);
popup.setWidth(width);
popup.setAdapter(adapter);
return popup;
}
使用例
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showListPopupWindow(view);
}
});