Android安卓用Dialog对话框简单并且完美实现popupWindow底部弹出效果,有动画效果

来源:互联网 发布:单片机软件工程师苏州 编辑:程序博客网 时间:2024/05/16 11:28

1背景

项目中经常用到popupwindow从底部弹出的样式,而且存在有动画,最初实现这种效果用是的popupwindow实现起来十分复杂(指的是动画要实现,效果要好),代码基类书写的代码量大。后来发现对话框可以实现从底部弹出效果,实验后发现,代码量奇迹般变少了,特分享一下思路和代码

2看效果

3 要点

主要用到了自定义对话框的进出动画,对话框样式,设置对话框的屏幕宽度和位置等相关的方法

4实现过程

A 在attr.xml文件里面定义对话框的样式

   <!-- 对话框风格 -->    <style name="dialogThemeBase" parent="@android:style/Theme.Dialog">        <!-- 窗口边框 -->        <item name="android:windowFrame">@null</item>        <!-- 窗口是否浮动在Activity上 -->        <item name="android:windowIsFloating">true</item>        <!-- 半透明 -->        <item name="android:windowIsTranslucent">false</item>        <!-- 没有标题栏 -->        <item name="android:windowNoTitle">true</item>        <!-- 背景 -->        <item name="android:background">@null</item>        <!-- 背景模糊 -->        <item name="android:backgroundDimEnabled">true</item>        <item name="android:windowBackground">@android:color/transparent</item>    </style>

B 在style.xml文件里面定义对话框菜单的进出样式


     <!-- 底部弹出popupwindow的动画样式 -->    <style name="popupAnimBottomIn" parent="android:Animation">        <item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item>        <item name="android:windowExitAnimation">@anim/slide_out_from_bottom</item>    </style>

C 在anin目录下创建两个动画文件

slide_in_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="300"    android:fromXDelta="0.0%"    android:fromYDelta="100.0%"    android:toXDelta="0.0%"    android:toYDelta="0.0%" />

slide_out_from_bottom.xml


<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="300"    android:fromXDelta="0.0%"    android:fromYDelta="0.0%"    android:toXDelta="0.0%"    android:toYDelta="100.0%" />

D 创建普通对话框基类

BaseDialog.java

package com.example.mytest20170113;import android.app.Dialog;import android.content.Context;import android.view.View;/** * Created by liugd on 2016/4/29. */public abstract class BaseDialog extends Dialog implements View.OnClickListener {    protected Context context;    public BaseDialog(Context context) {        this(context, R.style.dialogThemeBase);    }    public BaseDialog(Context context, int theme) {        super(context, theme);        this.context = context;        if (setWindowAnimation() != 0) {            getWindow().setWindowAnimations(setWindowAnimation());        }        setContentView(setView());        initListener();        finView();    }    /***     * 设置VIEW     *     * @return     */    protected abstract int setView();    protected void finView() {    }    //动画样式    protected int setWindowAnimation() {        return 0;    }    protected int[] setClickIDs() {        return null;    }    /***     * 泛型查找ID,无需强制转换     * @param id     * @return     */    @SuppressWarnings("unchecked")    protected <T extends View> T findViewByID(int id) {        return (T) super.findViewById(id);    }    /**     * 初始化点击事件     */    void initListener() {        int ids[] = setClickIDs();// 设置点击ID        if (ids != null && ids.length > 0) {            for (int id : ids) {                findViewById(id).setOnClickListener(this);            }        }    }}


E 底部弹出对话框基类
BaseBottomDialog.java

package com.example.mytest20170113;import android.content.Context;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.WindowManager;/** * 从底部弹出的对话框,实现popwindow弹出的效�? Created by liugd on 2017/1/12. */public abstract class BaseBottomDialog extends BaseDialog {public BaseBottomDialog(Context context) {super(context);configScreenSize(context);WindowManager.LayoutParams lp = getWindow().getAttributes();lp.width = mScreenWidth;getWindow().setAttributes(lp);getWindow().setGravity(Gravity.BOTTOM);// 底部展示}// 动画样式protected int setWindowAnimation() {return R.style.popupAnimBottomIn;}public static void configScreenSize(Context context) {if (mScreenWidth == 0) {DisplayMetrics display = new DisplayMetrics();WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);manager.getDefaultDisplay().getMetrics(display);mScreenHeight = Math.max(display.heightPixels, display.widthPixels);mScreenWidth = Math.min(display.heightPixels, display.widthPixels);}}public static int mScreenHeight;public static int mScreenWidth;}

F 底部对话框的实现类

MyTestBottomDialog.java

package com.example.mytest20170113;import android.content.Context;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MyTestBottomDialog extends BaseBottomDialog {public MyTestBottomDialog(Context context) {super(context);}@Overridepublic void onClick(View arg0) {Toast.makeText(context, ((TextView) arg0).getText().toString(), 0).show();switch (arg0.getId()) {case R.id.button1:break;case R.id.button2:break;case R.id.button3:break;}dismiss();}@Overrideprotected int[] setClickIDs() {return new int[] { R.id.button1, R.id.button2, R.id.button3 };}@Overrideprotected int setView() {return R.layout.dialog_bottom_test;}}

G  测试对话框的布局文件

dialog_bottom_test.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="选项1" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="选项2" />    <Button        android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="取消" /></LinearLayout>


H 在Activity里面测试

findViewById(R.id.textView1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {MyTestBottomDialog dialog = new MyTestBottomDialog(MainActivity.this);dialog.show();}});



2017-1-13 12:16

附CSDN下载地址

http://download.csdn.NET/detail/u012990509/9736702



1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高中生晚上偷着跑出去玩怎么办 货车高速忘记过安全检查站了怎么办 u盘上的文件名称乱码了怎么办 暖气管掉进去一点水泥渣怎么办 暗埋在瓷砖下的暖气管漏水怎么办 埋在瓷砖下面的水管漏水怎么办 埋在瓷砖下的水管漏水怎么办 水压太大把水管撑坏了怎么办 无效安装包与系统不兼容怎么办 圣空法师持五戒范戒了怎么办 美航空要是不改中国台湾标志怎么办 淘宝买的东西质量有问题怎么办 天猫618长达20天c店怎么办 新开的淘宝店铺没有生意怎么办 淘宝账号登陆限制用不了花呗怎么办 闲鱼买家签收后说是空盒怎么办 在咸鱼卖东西买家恶意退货怎么办 淘宝联系不上买家物流返回怎么办 换了支付宝绑定手机号退款怎么办啊 淘宝评价错了追评评价错了怎么办 淘宝给客户退款后还给差评怎么办 淘宝账号处于下单保护状态怎么办 淘宝卖家物流单号写错了怎么办 有个人给我发直播消息怎么办 网贷申请多了现在秒拒怎么办 顺丰快递寄的瓜果坏了怎么办 淘宝退货快递公司填错了怎么办 不小心把淘宝账号注销了怎么办 腾讯视频会员开通一个月贵怎么办 微交易买美国指数输了四千块怎么办 淘宝地址中包含了违禁词怎么办 微信支付失败但是钱扣了怎么办 支付宝向别人收款交易关闭了怎么办 从淘宝充的晋江币充值异常怎么办 接手转让店铺会员要求退卡怎么办 转转买手机卖家拒绝退款怎么办 淘宝买的东西电话号码留错了怎么办 平板电脑没电关机没保存文件怎么办 恢复出厂设置需要谷歌账号怎么办 华为手机云端里照片删除了怎么办 客户退货卖家一直没收到货怎么办