Android实现底部缓慢弹出菜单
程序员文章站
2023-12-17 18:33:16
项目要求要做一个从底部缓慢弹出一个button menu
开始编码:
myselffragment
package io.dcloud.h5b79...
项目要求要做一个从底部缓慢弹出一个button menu
开始编码:
myselffragment
package io.dcloud.h5b79c397.fragment; import android.content.context; import android.content.intent; import android.net.uri; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.relativelayout; import android.widget.toast; import com.android.volley.volleyerror; import com.vanda.vandalibnetwork.fragment.basefragment; import java.util.map; import cn.sharesdk.framework.sharesdk; import cn.sharesdk.onekeyshare.onekeyshare; import cn.sharesdk.sina.weibo.sinaweibo; import cn.sharesdk.tencent.qq.qq; import cn.sharesdk.wechat.friends.wechat; import cn.sharesdk.wechat.moments.wechatmoments; import io.dcloud.h5b79c397.r; import io.dcloud.h5b79c397.urls; import io.dcloud.h5b79c397.activity.bottommenu; import io.dcloud.h5b79c397.activity.loginactivity; import io.dcloud.h5b79c397.activity.myself_about_fae; import io.dcloud.h5b79c397.activity.myself_invitation; import io.dcloud.h5b79c397.activity.myself_help; import io.dcloud.h5b79c397.activity.myself_setting; import io.dcloud.h5b79c397.activity.registeractivity; import io.dcloud.h5b79c397.activity.user_management; import io.dcloud.h5b79c397.pojo.informmationdata; import io.dcloud.h5b79c397.util.actionbarutil; import io.dcloud.h5b79c397.util.extutils; /** * 用户个人信息界面 */ public class myselffragment extends basefragment<informmationdata> implements view.onclicklistener { private context mcontext; private view view; private bottommenu menuwindow; private relativelayout layout_advise, layout_collect, layout_logo, layout_share, layout_qr_code, layout_help, layout_set, layout_url; private button btnlogin, btnregister; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view = inflater.inflate(r.layout.fragment_information, container, false); initview(); return view; } @override public void onstart() { super.onstart(); } private void initview() { new actionbarutil(view, r.id.actionbar, r.id.back, r.id.title, r.id.activity, "我的管理中心", getactivity(), 2); layout_advise = (relativelayout) view.findviewbyid(r.id.myself_advise); layout_collect = (relativelayout) view.findviewbyid(r.id.myself_collect); layout_logo = (relativelayout) view.findviewbyid(r.id.myself_logo); layout_share = (relativelayout) view.findviewbyid(r.id.myself_share); layout_qr_code = (relativelayout) view.findviewbyid(r.id.myself_qr_code); layout_help = (relativelayout) view.findviewbyid(r.id.myself_help); layout_set = (relativelayout) view.findviewbyid(r.id.myself_set); layout_url = (relativelayout) view.findviewbyid(r.id.myself_url); btnlogin = (button) view.findviewbyid(r.id.myself_login); btnregister = (button) view.findviewbyid(r.id.myself_register); layout_advise.setonclicklistener(this); layout_collect.setonclicklistener(this); layout_logo.setonclicklistener(this); layout_share.setonclicklistener(this); layout_qr_code.setonclicklistener(this); layout_help.setonclicklistener(this); layout_set.setonclicklistener(this); layout_url.setonclicklistener(this); btnlogin.setonclicklistener(this); btnregister.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { case r.id.myself_advise: startactivity(new intent(getactivity(), user_management.class)); break; case r.id.myself_collect: break; case r.id.myself_logo: intent intent=new intent(getactivity(),myself_about_fae.class); startactivity(intent); break; case r.id.myself_share: menuwindow = new bottommenu(mcontext, clicklistener); menuwindow.show(); break; case r.id.myself_qr_code: startactivity(new intent(getactivity(), myself_invitation.class)); break; case r.id.myself_help: startactivity(new intent(getactivity(), myself_help.class)); break; case r.id.myself_set: startactivity(new intent(getactivity(), myself_setting.class)); break; case r.id.myself_url: uri uri = uri.parse("http://www.fae.cn/"); intent intent1 = new intent(intent.action_view, uri); startactivity(intent1); break; case r.id.myself_login: startactivity(new intent(getactivity(), loginactivity.class)); break; case r.id.myself_register: startactivity(new intent(getactivity(), registeractivity.class)); break; default: break; } } private view.onclicklistener clicklistener = new view.onclicklistener(){ public void onclick(view v) { switch (v.getid()) { case r.id.btn1: showzidingyi(wechat.name); break; case r.id.btn2: showzidingyi(wechatmoments.name); toast.maketext(mcontext,"这是微信朋友圈",toast.length_short).show(); break; case r.id.sina: showzidingyi(sinaweibo.name); break; case r.id.qq: showzidingyi(qq.name); break; case r.id.more: toast.maketext(mcontext,"这是更多",toast.length_short).show(); break; default: break; } } }; private void showzidingyi(string ss){ sharesdk.initsdk(getactivity()); onekeyshare oks = new onekeyshare(); oks.setsilent(false); oks.setdialogmode(); oks.settext("xxxxxxxxx"); //oks.setimageurl("http://sharesdk.cn"); oks.setplatform(ss); oks.show(getactivity()); } public static myselffragment newinstance(context context) { myselffragment fragment = new myselffragment(); fragment.mcontext = context; return fragment; } public myselffragment() { // required empty public constructor } @override protected void processdata(informmationdata response) { super.processdata(response); if (response != null) { } } @override protected void errordata(volleyerror volleyerror) { super.errordata(volleyerror); extutils.errorlog("-----myselffragment----volleyerror--------->", volleyerror + ""); } @override protected string getrequesturl() { return urls.url_prefix + ""; } @override protected class<informmationdata> getresponsedataclass() { return informmationdata.class; } @override protected map<string, string> getparammap() { return null; } }
bottommenu
package io.dcloud.h5b79c397.activity; import android.app.actionbar; import android.app.activity; import android.content.context; import android.graphics.drawable.colordrawable; import android.view.gravity; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.popupwindow; import io.dcloud.h5b79c397.r; import static io.dcloud.h5b79c397.r.id.btn1; import static io.dcloud.h5b79c397.r.id.btn2; /** * created by administrator on 2017/7/21 0021. */ public class bottommenu implements view.onclicklistener,view.ontouchlistener { private popupwindow popupwindow; private button wechat, friend,qq,sina,more ,btncancel; private view mmenuview; private activity mcontext; private view.onclicklistener clicklistener; public bottommenu(context context, view.onclicklistener clicklistener) { layoutinflater inflater = layoutinflater.from(context); this.clicklistener=clicklistener; mcontext= (activity) context; mmenuview = inflater.inflate(r.layout.layout_popwindow, null); wechat= (button) mmenuview.findviewbyid(btn1); friend= (button) mmenuview.findviewbyid(btn2); qq=(button)mmenuview.findviewbyid(r.id.qq) ; sina=(button)mmenuview.findviewbyid(r.id.sina); more=(button)mmenuview.findviewbyid(r.id.more); btncancel = (button) mmenuview.findviewbyid(r.id.btn_cancel); btncancel.setonclicklistener(this); wechat.setonclicklistener(this); friend.setonclicklistener(this); qq.setonclicklistener(this); sina.setonclicklistener(this); more.setonclicklistener(this); popupwindow=new popupwindow(mmenuview, actionbar.layoutparams.match_parent, actionbar.layoutparams.wrap_content,true); popupwindow.setanimationstyle(r.style.popwin_anim_style); colordrawable dw = new colordrawable(context.getresources().getcolor(r.color.apha)); popupwindow.setbackgrounddrawable(dw); mmenuview.setontouchlistener(this); } /** * 显示菜单 */ public void show(){ //得到当前activity的rootview view rootview=((viewgroup)mcontext.findviewbyid(android.r.id.content)).getchildat(0); popupwindow.showatlocation(rootview, gravity.bottom|gravity.center_horizontal, 0, 0); } @override public void onclick(view view) { popupwindow.dismiss(); switch (view.getid()) { case r.id.btn_cancel: break; default: clicklistener.onclick(view); break; } } @override public boolean ontouch(view arg0, motionevent event) { int height = mmenuview.findviewbyid(r.id.pop_layout).gettop(); int y=(int) event.gety(); if(event.getaction()==motionevent.action_up){ if(y<height){ popupwindow. dismiss(); } } return true; } }
layout_popwindow.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <linearlayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:gravity="center_horizontal" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:orientation="vertical" > <button android:background="#f2f2f2" android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="40dp" android:text="分享到微信好友" android:textcolor="@color/black" /> <button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="40dp" android:text="分享到微信朋友圈" android:layout_margintop="5dp" android:textcolor="@color/black" android:background="#f2f2f2" /> <button android:id="@+id/qq" android:layout_width="match_parent" android:layout_height="40dp" android:text="分享到qq" android:layout_margintop="5dp" android:textcolor="@color/black" android:background="#f2f2f2" /> <button android:id="@+id/sina" android:layout_width="match_parent" android:layout_height="40dp" android:text="分享到新浪微博" android:layout_margintop="5dp" android:textcolor="@color/black" android:background="#f2f2f2" /> <button android:id="@+id/more" android:layout_width="match_parent" android:layout_height="40dp" android:text="分享到更多..." android:layout_margintop="5dp" android:textcolor="@color/black" android:background="#f2f2f2" /> <button android:id="@+id/btn_cancel" android:layout_width="match_parent" android:layout_height="50dp" android:text="取消" android:layout_marginbottom="10dp" android:layout_margintop="15dp" android:background="#f2f2f2" android:alpha="100" android:textstyle="bold" /> </linearlayout> </relativelayout>
包anim//放在res目录下其中包括ppwindow_hide_anim.xml和ppwindow_show_anim.xml
其中ppwindow_hide_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromxdelta="0" android:toxdelta="0" android:fromydelta="0" android:toydelta="1920" /> <alpha android:duration="300" android:fromalpha="1" android:toalpha="0" /> </set>
其中ppwindow_show_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromxdelta="0" android:toxdelta="0" android:fromydelta="1920" android:toydelta="0" /> <alpha android:duration="300" android:fromalpha="0" android:toalpha="1" /> </set>
最后上图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。