Android实现微信右侧顶部下拉对话框
程序员文章站
2022-08-20 16:17:10
我们使用微信都知道,其右侧顶部有一个下拉对话框,我们可以执行添加好友,扫一扫等功能,今天我们就来模仿实现一下这个功能(实现的方式有很多种,我今天只说一种借助透明主题acti...
我们使用微信都知道,其右侧顶部有一个下拉对话框,我们可以执行添加好友,扫一扫等功能,今天我们就来模仿实现一下这个功能(实现的方式有很多种,我今天只说一种借助透明主题activity的方式实现;如果有兴趣还可以移步至);本篇的实现的效果如下:
下面就来说一说实现的思路(重要):
第一步:创建弹出对话框布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <relativelayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margintop="45dp" android:layout_marginright="20dp"> <linearlayout android:id="@+id/id_pop_dialog_layout" android:layout_width="@dimen/pop_list_width" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:background="@drawable/pop_item_normal" android:orientation="vertical" > <linearlayout android:id="@+id/upload_record_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:layout_margintop="5dp" android:background="@drawable/pop_list_selector" > <imageview android:id="@+id/id_imageview1" android:layout_width="@dimen/pop_dialog_icon_size" android:layout_height="@dimen/pop_dialog_icon_size" android:layout_gravity="center_vertical" android:layout_marginleft="8dp" android:src="@drawable/upload_icon_record" /> <textview android:layout_width="@dimen/pop_list_width" android:layout_height="wrap_content" android:padding="8dp" android:text="@string/uploadrecord" android:layout_gravity="center_vertical" android:textcolor="#fff" android:textsize="16sp" /> </linearlayout> <imageview android:id="@+id/id_imageview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> <linearlayout android:id="@+id/register_record_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:background="@drawable/pop_list_selector" > <imageview android:id="@+id/id_imageview2" android:layout_width="@dimen/pop_dialog_icon_size" android:layout_height="@dimen/pop_dialog_icon_size" android:layout_gravity="center_vertical" android:layout_marginleft="8dp" android:src="@drawable/register_icon_record" /> <textview android:layout_width="@dimen/pop_list_width" android:layout_height="wrap_content" android:padding="8dp" android:text="@string/registerrecord" android:layout_gravity="center_vertical" android:textcolor="#fff" android:textsize="16sp" /> </linearlayout> <imageview android:id="@+id/id_imageview7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> <linearlayout android:id="@+id/new_massage_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginleft="5dp" android:layout_marginright="5dp" android:background="@drawable/pop_list_selector" > <imageview android:id="@+id/id_imageview3" android:layout_width="@dimen/pop_dialog_icon_size" android:layout_height="@dimen/pop_dialog_icon_size" android:layout_gravity="center_vertical" android:layout_marginleft="8dp" android:src="@drawable/message_icon_tip" /> <textview android:id="@+id/new_message" android:layout_width="@dimen/pop_list_width" android:layout_height="wrap_content" android:padding="8dp" android:text="@string/defaultmessage" android:layout_gravity="center_vertical" android:textcolor="#fff" android:textsize="16sp" /> </linearlayout> <imageview android:id="@+id/id_imageview6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> </linearlayout> </relativelayout> </relativelayout>
第二步:创建一个用于显示该对话框布局activity
package com.hfut.popdialogtest; import android.app.activity; import android.os.bundle; import android.view.motionevent; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.widget.linearlayout; /** * @author why * @date 2018-10-3 */ public class mydialogactivity extends activity implements onclicklistener{ private linearlayout uploadrecord; private linearlayout registerrecord; private linearlayout newmessage; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.pop_dialog); if(getactionbar()!=null){ getactionbar().hide(); } commontools.setnavbarvisibility(this); initview(); } private void initview(){ uploadrecord = findviewbyid(r.id.upload_record_layout); registerrecord = findviewbyid(r.id.register_record_layout); newmessage = findviewbyid(r.id.new_massage_layout); uploadrecord.setonclicklistener(this); registerrecord.setonclicklistener(this); newmessage.setonclicklistener(this); } @override public boolean ontouchevent(motionevent event){ finish(); return true; } @override public void onclick(view v) { switch (v.getid()){ case r.id.upload_record_layout: shareddata.resultid=1; break; case r.id.register_record_layout: shareddata.resultid=2; break; case r.id.new_massage_layout: shareddata.resultid=3; break; default: shareddata.resultid=0; break; } this.finish(); } }
第三步:创建一个主界面
mainactivity.java代码:
package com.hfut.popdialogtest; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.textview; /** * @author why * @date 2018-10-3 9:35:35 */ public class mainactivity extends appcompatactivity { textview resultshow; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); resultshow = findviewbyid(r.id.show_choosen_result); if(getactionbar()!=null){ getactionbar().hide(); } commontools.setnavbarvisibility(this); } @override protected void onresume() { switch (shareddata.resultid) { case 0: resultshow.settext("默认显示"); break; case 1: resultshow.settext(getresources().getstring(r.string.uploadrecord)); break; case 2: resultshow.settext(getresources().getstring(r.string.registerrecord)); break; case 3: resultshow.settext(getresources().getstring(r.string.defaultmessage)); break; default: resultshow.settext("默认显示"); break; } super.onresume(); } public void openpopdialog(view view) { intent intent = new intent(this, popdialogactivity.class); startactivity(intent); } }
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout 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="com.hfut.popdialogtest.mainactivity"> <imageview android:onclick="openpopdialog" android:id="@+id/pop_dialog_icon" app:layout_constraintright_torightof="parent" android:layout_marginright="10dp" app:layout_constrainttop_totopof="parent" android:layout_margintop="5dp" android:background="@drawable/message_tip" android:layout_width="50dp" android:layout_height="50dp" /> <textview android:gravity="center" android:textcolor="@color/coloraccent" android:textsize="30sp" android:id="@+id/show_choosen_result" app:layout_constrainttop_tobottomof="@id/pop_dialog_icon" android:layout_margintop="50dp" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.constraint.constraintlayout>
第四步:设置对话框activity主题为透明主题
androidmanifest.xml文件代码:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hfut.popdialogtest"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundicon="@mipmap/ic_launcher_round" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".mydialogactivity" android:theme="@android:style/theme.translucent" /> </application> </manifest>
第五步:其他辅助代码
commontools.java代码:
package com.hfut.popdialogtest; import android.app.activity; import android.view.view; /** * author:why * created on: 2018/9/11 13:34 * description: */ public class commontools { /** * to controll the visibility of the activity's navigator bar * @param activity */ public static void setnavbarvisibility(activity activity) { view decorview = activity.getwindow().getdecorview(); decorview.setsystemuivisibility( view.system_ui_flag_layout_stable | view.system_ui_flag_layout_hide_navigation | view.system_ui_flag_layout_fullscreen | view.system_ui_flag_hide_navigation | view.system_ui_flag_fullscreen | view.system_ui_flag_immersive_sticky); } }
values目录下的dimens.xml代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="pop_list_width">160dp</dimen> <dimen name="pop_dialog_icon_size">60dp</dimen> <dimen name="pop_dialog_icon_tip_size">40dp</dimen> </resources>
values目录下的strings.xml代码:
<resources> <string name="app_name">仿微信右侧顶部下拉弹出测试</string> <string name="uploadrecord">上传记录</string> <string name="registerrecord">注册记录</string> <string name="defaultmessage">消息提示</string> </resources>
其他资源文件就不添加了。我们总结一下其实就是这样的步骤:
- 点击主activity的弹窗对话框图标,打开一个新的透明的acitivity
- 在新的activity中做完逻辑处理把结果存放在主activity可访问的数据域,然后finish自己
- 主activity再次可交互,并在onresume中实现对处理结果分析和处理,比如修改主activity ui;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: python-day3(正式学习)
下一篇: 探访世界上9大稀奇古怪的地标建筑