Android 自定义弹出菜单(布局)
我正在尝试升级我的弹出式菜单,使其具有图标和自定义样式。Android 自定义弹出菜单(布局),android,layout,drop-down-menu,android-custom-view,popupmenu,Android,Layout,Drop Down Menu,Android Custom View,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">
我已经为它创建了一个新的布局
<?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>
我希望弹出菜单是定制的(这个布局),如图所示
A是用来显示的,没有一种好的方法可以自定义菜单项的外观。如果你想要更灵活的东西,你的答案是
private静态最终字符串TITLE=“TITLE”;
私有静态最终字符串ICON=“ICON”;
私有列表数据=新的ArrayList();
//使用此选项可将项目添加到ListPopupWindow将使用的列表中
私有void附加项(字符串标题,int-iconResourceId){
HashMap=newHashMap();
地图放置(标题,标题);
map.put(图标,iconResourceId);
数据。添加(地图);
}
//如果要显示ListPopupWindow,请调用此选项
私有void showListMenu(查看锚定){
ListPopupWindow-popupWindow=新ListPopupWindow(此);
ListAdapter=新的SimpleAdapter(
这
数据,
android.R.layout.activity\u list\u item,//您可能需要使用自己的酷布局
新字符串[]{TITLE,ICON},//这些只是数据使用的键
new int[]{android.R.id.text1,android.R.id.icon});//要将数据映射到的视图id
popupWindow.setAnchorView(锚);
popupWindow.setAdapter(适配器);
setWidth(400);//注意:不要使用像素,使用dimen资源
popupWindow.setOnItemClickListener(myListener);//选择列表项时的回调
popupWindow.show();
}
这是我用自定义适配器为自定义ListPopupWindow
做的演示
型号
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);
}
}
}
示例使用
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showListPopupWindow(view);
}
});
这是我第一次尝试这样做,你能给我举个例子说明我是如何做到的吗?我使用了没有列表菜单的弹出窗口(自定义布局),现在我在定位上有问题,X坐标不起作用。你能检查一下这个吗?参考此选项可获得更简单的自定义文本颜色/背景修复。要使ListPopupWindow显示在屏幕键盘上方,请添加
ListPopupWindow.setInputMethodMode(不需要PopupWindow.INPUT方法)代码>。此外,还有一种方法可以自定义窗口的动画,如下所述:@Mr IDE感谢您在使用带有软键盘的ListPopupWindow时共享解决方案
<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>
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);
}
});