Android 自定义PopupWindow实现一个右侧滑入的弹窗dialog

在这里插入图片描述
对于底部栏,有Android谷歌支持的 BottomSheetDialogFragment基本用法
对于右边栏,可以通过自定义PopupWindow来实现。

MainActivity中的调用

    private void showRightDialog() {
        PopupWindowRight popupWindowRight = new PopupWindowRight(this);
        popupWindowRight.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT | Gravity.BOTTOM, 0, 0);
    }

PopupWindowRight(具体的实现类)

package com.zhangyu.bottomsheetdialog;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class PopupWindowRight extends PopupWindow {
    private static final String TAG = "PopupWindowRight";

    public PopupWindowRight(Context context) {
        //设置view
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.popup_dialog_right, null);
        setContentView(view);
        initView();
        //activity的contentView的高度
        int height = ((Activity) context).findViewById(android.R.id.content).getHeight();
        //其他设置
        setWidth(dp2px(100));//必须设置宽度
        setHeight(height);//必须设置高度
        setFocusable(false);//是否获取焦点
        setOutsideTouchable(true);//是否可以通过点击屏幕外来关闭
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        //加入动画
        ObjectAnimator.ofFloat(getContentView(), "translationX", getWidth(), 0).setDuration(500).start();
    }

    /**
     * 获取屏幕宽高
     *
     * @param context
     * @return
     */
    private static int[] getScreenSize(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
    }

    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void initView() {

    }
}


popup_dialog_right.xml (布局)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fl_root"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="hello world" />

</FrameLayout>

可以设置在ToolBar上层显示

只需要将高度设置成屏幕的高度

        //activity的contentView的高度
        //int height = ((Activity) context).findViewById(android.R.id.content).getHeight();
        //屏幕的高度
        int height = getScreenSize(context)[1];

在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android弹窗dialog可以通过使用PopupWindowDialog实现PopupWindow一个在屏幕上方显示的浮动控件,而Dialog一个模态对话。对于PopupWindow的使用,可以通过创建一个布局文件,然后在代码中使用PopupWindow类来显示该布局。而对于Dialog的使用,可以通过创建一个AlertDialog.Builder对象,设置对话的标题、内容和按钮等属性,最后调用show()方法显示对话。 下面是一个使用PopupWindow的示例代码: ```java // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置要显示的布局 View view = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(view); // 设置PopupWindow的宽度和高度 popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); // 设置PopupWindow的背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置PopupWindow是否可点击 popupWindow.setTouchable(true); // 设置PopupWindow是否获取焦点 popupWindow.setFocusable(true); // 设置PopupWindow的位置 popupWindow.showAtLocation(anchorView, Gravity.CENTER, 0, 0); ``` 下面是一个使用Dialog的示例代码: ```java // 创建AlertDialog.Builder对象 AlertDialog.Builder builder = new AlertDialog.Builder(context); // 设置对话的标题 builder.setTitle("提示"); // 设置对话的内容 builder.setMessage("这是一个对话"); // 设置对话的按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮的逻辑处理 } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮的逻辑处理 } }); // 创建并显示对话 AlertDialog dialog = builder.create(); dialog.show(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值