Android中Dialog自定义上下文花式菜单
程序员文章站
2023-12-13 08:22:04
这是一个类似于qq里面长按消息弹出菜单的效果,菜单跟着item动
闲话不说,上代码
先自定义一个dialog
import android.app.d...
这是一个类似于qq里面长按消息弹出菜单的效果,菜单跟着item动
闲话不说,上代码
先自定义一个dialog
import android.app.dialog; import android.content.context; import android.os.bundle; import android.view.view; import android.widget.textview; import io.dcloud.h5b79c397.r; /** * created by administrator on 2017/9/19 0019. */ public class selfdialog extends dialog { private textview copy;//确定按钮 private textview del;//取消按钮 private onnoonclicklistener noonclicklistener;//取消按钮被点击了的监听器 private onyesonclicklistener yesonclicklistener;//确定按钮被点击了的监听器 public selfdialog(context context) { super(context, r.style.mydialog); } public void setnoonclicklistener(string str, onnoonclicklistener onnoonclicklistener) { if (str != null) { //nostr = str; } this.noonclicklistener = onnoonclicklistener; } public void setyesonclicklistener(string str, onyesonclicklistener onyesonclicklistener) { if (str != null) { //yesstr = str; } this.yesonclicklistener = onyesonclicklistener; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_noticeactivity_dialog); //按空白处不能取消动画 setcanceledontouchoutside(true); //初始化界面控件 initview(); //初始化界面数据 //初始化界面控件的事件 initevent(); } private void initevent() { //设置确定按钮被点击后,向外界提供监听 del.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (yesonclicklistener != null) { yesonclicklistener.onyesclick(); } } }); //设置取消按钮被点击后,向外界提供监听 copy.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (noonclicklistener != null) { noonclicklistener.onnoclick(); } } }); } private void initview() { del = (textview) findviewbyid(r.id.del); copy = (textview) findviewbyid(r.id.text); } public interface onyesonclicklistener { public void onyesclick(); } public interface onnoonclicklistener { public void onnoclick(); } }
然后看dialog里面的布局文件
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <textview android:text="复制" android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:drawabletop="@mipmap/collect"/> <textview android:layout_marginleft="15dp" android:clickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:id="@+id/del" android:drawabletop="@mipmap/myself"/> </linearlayout>
还给dialog设置了一个样式
<style name="mydialog" parent="android:style/theme.dialog"> <!--背景颜色及和透明程度--> <item name="android:windowbackground">@android:color/transparent</item> <!--是否去除标题 --> <item name="android:windownotitle">true</item> <!--是否去除边框--> <item name="android:windowframe">@null</item> <!--是否浮现在activity之上--> <item name="android:windowisfloating">true</item> <!--是否模糊--> <item name="android:backgrounddimenabled">false</item> </style>
然后看主代码
public class menutestactivity extends activity { private listview listview; private selfdialog selfdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_menu_test); listview = (listview) findviewbyid(r.id.list); final arrayadapter<string> adapter = new arrayadapter<string>(menutestactivity.this, android.r.layout.simple_list_item_1); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); adapter.add("1"); listview.setadapter(adapter); listview.setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(final adapterview<?> parent, final view view, final int position, long id) { int[] location = new int[2]; // parent.getitematposition(position).getlocationonscreen(location); view.getlocationonscreen(location);//当前item的坐标 int x = location[0]; int y = location[1]; system.out.println("x:"+x+"y:"+y);//获取控件位置坐标可采用以上方法 selfdialog = new selfdialog(menutestactivity.this); // window window = selfdialog.getwindow(); // window.setcontentview(view); window dialogwindow = selfdialog.getwindow(); windowmanager.layoutparams lp = dialogwindow.getattributes(); dialogwindow.setgravity(gravity.left | gravity.top); lp.x = location[0]+80; // 新位置x坐标 lp.y = location[1]-30; // 新位置y坐标 // 当window的attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setattributes // dialog.onwindowattributeschanged(lp); dialogwindow.setattributes(lp); selfdialog.setcanceledontouchoutside(true);// 设置点击屏幕dialog不消失 selfdialog.setyesonclicklistener("确定", new selfdialog.onyesonclicklistener() { @override public void onyesclick() { toast.maketext(menutestactivity.this, adapter.getitem(position), toast.length_long).show(); selfdialog.dismiss(); } }); selfdialog.setnoonclicklistener("取消", new selfdialog.onnoonclicklistener() { @override public void onnoclick() { toast.maketext(menutestactivity.this, adapter.getitem(position), toast.length_long).show(); selfdialog.dismiss(); } }); selfdialog.show(); return true; } }); } }
主程序的布局
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="io.dcloud.h5b79c397.testactivity.menutestactivity"> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/list"></listview> </linearlayout>
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。