Android自定义PopupWindow,高仿炫酷的IOS对话框

前言:

 最近在使用IOS系统的过程中发现IOS底部弹出框甚是漂亮,大气,上档次,于是乎就想啊能不能在Android中实现类似的对话框呢?你说,这不是废话吗,除了一些极少数的系统级的不能模仿外(版权)还有啥不能依瓢画葫芦的呢,所以啊,这篇文章将介绍如何在Android中实现高仿IOS对话框效果,先上图,给大家养养眼:

这里写图片描述

大家在看到上面的对话框时有没有想到简单的实现思路呢?我这里给出的思路是我们可以自定义一个PopupWindow,然后设置我们的布局。这里的布局很有技巧哦,那就是对话框中间的透明隔断区域其实是一个margin值,每个隔断的item layout的背景为一个白色圆角矩形,之后再让PopupWindow的背景为透明即可,是不是很简单呢。好了,让我们动手编写代码将它带回家吧。

大家也可以看看我的上篇文章:Android自定义Dialog,炫酷主流的加载对话框

代码实现

1. 编写布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:paddingTop="12dp"
        android:paddingBottom="12dp"
        android:background="@drawable/corner_white_bg"
        android:layout_height="wrap_content">

        <Button
            android:id="@id/btn_share_weixin"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_share_weixin"
            android:drawablePadding="6dp"
            android:background="@null"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_14_sp"
            android:text="@string/weixin"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@id/btn_share_friends"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_share_friends"
            android:drawablePadding="6dp"
            android:background="@null"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_14_sp"
            android:text="@string/weixin_friends"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@id/btn_share_qq"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_share_qq"
            android:drawablePadding="6dp"
            android:background="@null"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_14_sp"
            android:text="@string/qq"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@id/btn_share_qq_zone"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:drawableTop="@mipmap/ic_share_zones"
            android:drawablePadding="6dp"
            android:textColor="@android:color/black"
            android:textSize="@dimen/text_14_sp"
            android:background="@null"
            android:text="@string/qq_zones"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <Button
        android:id="@id/btn_cancel"
        android:layout_width="match_parent"
        android:textColor="@android:color/black"
        android:textSize="@dimen/text_14_sp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:text="@string/cancel"
        android:background="@drawable/corner_white_bg"
        android:layout_height="wrap_content"/>
</LinearLayout>

这里被隔断的部分有两个,所以布局中有两个view的背景为白色圆角矩形。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@android:color/white"/>
</shape>

2. 继承PopupWindow

public class IosPopupWindow extends PopupWindow implements View.OnClickListener {

    private Context mContext;

    public IosPopupWindow(Activity activity) {
        super(activity);
        mContext = activity;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.dialog_share, null);
        setContentView(contentView);
        int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
        //获取popupwindow的高度与宽度
        this.setWidth((int) (screenWidth - 2 * dp2px(mContext,12f)));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置背景透明度
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置动画
        this.setAnimationStyle(R.style.IosDialog);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        // 点击外部可取消
        this.setOutsideTouchable(true);
        initView(contentView);
    }

以上代码最关键的就是给我们的PopupWindow设置一个透明的背景Drawable啦。

3. 窗口弹出时让外部变暗

/**
 * 让popupwindow以外区域阴影显示
 */
private void popOutShadow() {
    final Window window = ((Activity) mContext).getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.alpha = 0.5f;//设置阴影透明度
    window.setAttributes(lp);
    setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss() {
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.alpha = 1f;
            window.setAttributes(lp);
        }
    });
}

与Dialog不同的是PopupWindow实现外部变暗需通过改变它依附的window的透明度,所以我们传给PopupWindow的Context需为Activity类型,同时在窗口消失的时候记得将Window的透明度重置。

最后,奉上IosPopupWindow的github,你值得拥有:https://github.com/ydxlt/LoadingDialog

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您需要更高级的弹窗样式和交互时,可以使用 PopupWindow 来创建自定义布局的弹窗。下面是使用 PopupWindow 创建自定义布局的步骤: 1. 创建自定义布局文件:首先,创建一个 XML 文件来定义您的自定义布局。例如,您可以创建一个名为 `custom_popup.xml` 的文件,并在其中定义您希望显示的布局。 2. 实例化 PopupWindow:在您的 Activity 或 Fragment 中,实例化 PopupWindow 对象。 ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.custom_popup, null); PopupWindow popupWindow = new PopupWindow(customView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ``` 3. 设置 PopupWindow 属性:根据需要,设置 PopupWindow 的属性,例如背景、动画效果、焦点等。 ```java popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置动画效果 popupWindow.setAnimationStyle(R.style.PopupAnimation); ``` 4. 设置布局中的控件和事件:通过 `customView` 获取布局中的控件,并设置相应的事件监听器。 ```java Button button = customView.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 } }); ``` 5. 显示弹窗:使用 `showAtLocation()` 或 `showAsDropDown()` 方法显示弹窗。`showAtLocation()` 方法可以显示在指定的位置,而 `showAsDropDown()` 方法则可以显示在某个视图的下方。 ```java View anchorView = findViewById(R.id.anchor_view); // 锚点视图 popupWindow.showAsDropDown(anchorView); // 或者使用 showAtLocation() 方法 ``` 这样,您就可以使用 PopupWindow 创建自定义布局的弹窗。希望对您有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值