Android-气泡对话框(根据被点击View位置显示、可定制)

Go Github

气泡布局的形状可以改变,如四角弧度、气泡颜色、箭头大小和阴影。

气泡Dialog可以根据被点击的view的位置来确定自己展示的位置。

更新

  • 1.1.0
    ①Dialog交互事件传递到Activity达到不在不关闭Dialog的情况下做其他Activity的操作。
    ②添加自动根据被点击View距离屏幕边缘的距离确定Dialog的位置。
    ③新增“autoPosition”和“setThroughEvent”方法,请参考“BubbleDialog方法参考表”

    08154648_dZeX.jpg

     

  • 1.0.3:继续优化了点击在气泡之外才会被dismiss;修复了Dialog周围会有部分点击无法dismiss;

  • 1.0.2:修复点击dialog边缘无法取消

如何开始?

在你模块中的build.gradle添加上HappyBubble依赖

compile 'com.github.xujiaji:happy-bubble:1.1.0'

如何使用 HappyBubble-BubbleDialog?

方法参考表

方法名参数描述
addContentViewView添加填充在气泡中的视图
setClickedViewView被点击的View(触发Dialog出现的View)
setPositionenum BubbleDialog.Position:LEFT, TOP, RIGHT, BOTTOMBubbleDialog相对于被点击的view的位置
calBarboolean是否计算状态栏的高度(如果布局没有全屏,则需要计算)
setOffsetXint如果您对dialog所展示的x轴位置不满,需要调整x轴方向偏移
setOffsetYint如果您对dialog所展示的y轴位置不满,需要调整y轴方向偏移
setBubbleLayoutBubbleLayout自定义dialog的气泡布局
setTransParentBackground-背景透明
softShowUp-当气泡dialog中有EditText时,软键盘弹出会遮挡EditText时,dialog随软键盘上移。
show-显示
autoPositionboolean是否开启自动确定位置功能,开启后,“setPosition”功能失效
setThroughEventboolean, boolean第一个参数isThroughEvent设置是否穿透Dialog手势交互。
第二个参数cancelable 点击空白是否能取消Dialog,只有当"isThroughEvent = false"时才有效

最简单的实现

  

exampel1

exampel2

需要提供:Context、填充的View、被点击的View。
如果最外层布局没有全屏时,您需要计算状态栏的高度,否则会多向下偏移一个状态栏的高度。

new BubbleDialog(this)
        .addContentView(LayoutInflater.from(this).inflate(R.layout.dialog_view3, null))
        .setClickedView(mButton)
        .calBar(true)
        .show();

向下偏移8dp

 

exampel3

 

new BubbleDialog(this)
        .addContentView(LayoutInflater.from(this).inflate(R.layout.dialog_view3, null))
        .setClickedView(mButton4)
        .setPosition(mPosition)
        .setOffsetY(8)
        .calBar(true)
        .show();

当想要输入框随软键盘上移时

 

exampel4

 

new BubbleDialog(this)
        .addContentView(LayoutInflater.from(this).inflate(R.layout.dialog_view, null))
        .setClickedView(mButton12)
        .setPosition(mPosition)
        .calBar(true)
        .softShowUp()
        .show();

自定义 BubbleLayout.

 

exampel5

 

BubbleLayout bl = new BubbleLayout(this);
bl.setBubbleColor(Color.BLUE);
bl.setShadowColor(Color.RED);
bl.setLookLength(Util.dpToPx(this, 54));
bl.setLookWidth(Util.dpToPx(this, 48));
new BubbleDialog(this)
        .addContentView(LayoutInflater.from(this).inflate(R.layout.dialog_view5, null))
        .setClickedView(mButton8)
        .setPosition(mPosition)
        .calBar(true)
        .setBubbleLayout(bl)
        .show();

自定义 BubbleDialog,可交互的 BubbleDialog.

 

exampel6

 

1、布局

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

    <Button
        android:id="@+id/button13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/button14"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button2" />

    <Button
        android:id="@+id/button15"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button3" />

</LinearLayout>

2、自定义 BubbleDialog


/**
 * 自定义可操作性dialog
 * Created by JiajiXu on 17-12-11.
 */

public class CustomOperateDialog extends BubbleDialog implements View.OnClickListener
{
    private ViewHolder mViewHolder;
    private OnClickCustomButtonListener mListener;

    public CustomOperateDialog(Context context)
    {
        super(context);
        calBar(true);
        setTransParentBackground();
        setPosition(Position.TOP);
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_view4, null);
        mViewHolder = new ViewHolder(rootView);
        addContentView(rootView);
        mViewHolder.btn13.setOnClickListener(this);
        mViewHolder.btn14.setOnClickListener(this);
        mViewHolder.btn15.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        if (mListener != null)
        {
            mListener.onClick(((Button)v).getText().toString());
        }
    }

    private static class ViewHolder
    {
        Button btn13, btn14, btn15;
        public ViewHolder(View rootView)
        {
            btn13 = rootView.findViewById(R.id.button13);
            btn14 = rootView.findViewById(R.id.button14);
            btn15 = rootView.findViewById(R.id.button15);
        }
    }

    public void setClickListener(OnClickCustomButtonListener l)
    {
        this.mListener = l;
    }

    public interface OnClickCustomButtonListener
    {
        void onClick(String str);
    }
}

3、显示

CustomOperateDialog codDialog = new CustomOperateDialog(this)
        .setPosition(mPosition)
        .setClickedView(mButton10);
codDialog.setClickListener(new CustomOperateDialog.OnClickCustomButtonListener()
{
    @Override
    public void onClick(String str)
    {
        mButton10.setText("点击了:" + str);
    }
});
codDialog.show();

查看关于BappyDialog的使用代码

TestDialogActivity 代码

如何使用 HappyBubble-BubbleLayout?

在XML代码中设置属性值

属性参照表

属性描述
lookAtleft, top, right, bottom箭头指向
lookLengthdimension箭头的长度
lookPositiondimension箭头相对于x或y轴的位置
lookWidthdimension箭头的宽度
bubbleColorcolor气泡的颜色
bubbleRadiusdimension气泡四角的圆弧
bubblePaddingdimension气泡边缘到内容的距离
shadowRadiusdimension阴影的扩散大小
shadowXdimension阴影在x轴方向的偏移
shadowYdimension阴影在y轴方向的偏移
shadowColorcolor阴影的颜色

xml 例子

    <com.xujiaji.happybubble.BubbleLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bubbleLayout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="16dp"
        app:lookAt="left"
        app:lookLength="16dp"
        app:lookPosition="20dp"
        app:lookWidth="16dp" />

在java代码中定义属性值。

BubbleLayout 通过“set属性名”方法和invalidate方法来更新BubbleLayout。

mBubbleLayout.setLook(BubbleLayout.Look.LEFT);

查看更多

MainActivity 代码

 

GIF

 

Demo 下载

 

GitHub release

 

感谢您的使用、Star与建议!


作者:灵魂奏响曲
链接:https://juejin.im/post/5a333f0af265da431523f408
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

转载于:https://my.oschina.net/sfshine/blog/1604010

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值