码迷,mamicode.com
首页 > 其他好文 > 详细

自定义Dialog

时间:2017-10-18 16:44:05      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:efault   ram   logs   oncreate   layout   自定义style   als   contex   super   

一: 效果图

技术分享   技术分享

 

二:继承Dialog

1.居中弹出

技术分享
package com.xiaoguaishou.customdialog;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * @describe 自定义居中弹出dialog
 */
public class CenterDialog extends Dialog implements View.OnClickListener {

    private Context context;

    private int layoutResID;

    private View contentView;

    /**
     * 要监听的控件id
     */
    private int[] listenedItems;

    private OnCenterItemClickListener listener;

    public CenterDialog(Context context, int layoutResID, int[] listenedItems) {
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }

    public CenterDialog(Context context, View contentView, int[] listenedItems) {
        super(context, R.style.dialog_custom);
        this.context = context;
        this.contentView = contentView;
        this.listenedItems = listenedItems;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
//        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        if (contentView != null)
            setContentView(contentView);
        else
            setContentView(layoutResID);
        // 宽度全屏
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * 4 / 5; // 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        // 点击Dialog外部消失
        setCanceledOnTouchOutside(true);

        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }
    }

    public interface OnCenterItemClickListener {
        void OnCenterItemClick(CenterDialog dialog, View view);
    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        dismiss();
        listener.OnCenterItemClick(this, view);
    }
}
CenterDialog

2.底部弹出

技术分享
package com.xiaoguaishou.customdialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class BottomDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private int layoutResID;
    private View contentView;

    /**
     * 要监听的控件id
     */
    private int[] listenedItems;

    private OnBottomMenuItemClickListener listener;

    public BottomDialog(Context context, int layoutResID, int[] listenedItems) {
        super(context, R.style.dialog_custom);
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }

    public BottomDialog(Context context, View contentView, int[] listenedItems) {
        super(context, R.style.dialog_custom);
        this.context = context;
        this.contentView = contentView;
        this.listenedItems = listenedItems;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画

        if (contentView != null)
            setContentView(contentView);
        else
            setContentView(layoutResID);
        // 宽度全屏
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * 9 / 10; // 设置宽度
        getWindow().setAttributes(lp);
        // 点击Dialog外部消失
        setCanceledOnTouchOutside(true);

        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }
    }

    public interface OnBottomMenuItemClickListener {
        void onBottomMenuItemClick(BottomDialog dialog, View view);
    }

    public void setOnBottomMenuItemClickListener(OnBottomMenuItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        dismiss();
        listener.onBottomMenuItemClick(this, view);
    }

}
BottomDialog

 

三:自定义style

技术分享
<!-- dialog底部弹出菜单动画 -->
    <style name="bottom_menu_animation" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/bottom_menu_enter</item>
        <item name="@android:windowExitAnimation">@anim/bottom_menu_exit</item>
    </style>
    <!-- dialog样式 -->
    <style name="dialog_custom" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>    <!--是否浮在界面上-->
        <item name="android:windowIsTranslucent">true</item> <!--是否半透明-->
        <item name="android:windowNoTitle">false</item>       <!--是否有标题-->
        <item name="android:windowBackground">@android:color/transparent</item> <!--窗口背景色透明-->
        <item name="android:backgroundDimEnabled">false</item> <!--背景是否模糊显示-->
    </style>
style文件中

 

 四:弹入弹出动画

技术分享
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="400"
        android:fromYDelta="100%p" />
</set>
弹入动画
技术分享
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="600"
        android:toYDelta="100%p" />
</set>
弹出动画

 

五:最后贴一个代码结构

技术分享

 

源码地址

 

自定义Dialog

标签:efault   ram   logs   oncreate   layout   自定义style   als   contex   super   

原文地址:http://www.cnblogs.com/bky1225987336/p/7687359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!