DialogFragment你值得拥用

最近写了一个弹框,发现之前写弹窗方式。

  1. dialog
  2. PopupWindow

因为Android 官方推荐使用 DialogFragment,所以就尝试了一下,简直不要太好啊。

DialogFragment是基于Fragment实现的。

public class ShareGiftBag extends DialogFragment implements View.OnClickListener {

    private TextView tvTitle;
    private TextView tvBut;
    private ImageView ivPic, ivCancel;
    private Callback callback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.common_share_giftbag_dialog, container, false);
        initView(v);
        return v;
    }

    private void initView(View v) {
        tvTitle = v.findViewById(R.id.tv_title);
        tvBut = v.findViewById(R.id.tv_but);
        ivPic = v.findViewById(R.id.iv_pic);
        ivCancel = v.findViewById(R.id.iv_cancel);
        tvBut.setOnClickListener(this);
        ivCancel.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_cancel) {
            dismiss();
        } else if (id == R.id.tv_but) {
            if (callback != null) {
                callback.call("");
                ToastUtil.s("1234567890");
            }
        }
    }

    //显示信息
    public void setDataShow(String title, Callback callback) {
        this.callback = callback;
//        tvTitle.setText(title);
    }
}


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="307dp"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:background="@drawable/round_corner_white_10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="15dp"
        android:gravity="center"
        android:text="礼物分享你我他"
        android:textColor="#333333"
        android:textSize="@dimen/lyg_font_size_17sp" />

    <ImageView
        android:id="@+id/iv_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:src="@drawable/icon_card_icon_close" />

    <ImageView
        android:id="@+id/iv_pic"
        android:layout_width="263dp"
        android:layout_height="179dp"
        android:layout_below="@+id/tv_title"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="7dp"
        android:layout_marginRight="22dp"
        android:src="@drawable/icon_pic_gift" />


    <TextView
        android:id="@+id/tv_but"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/iv_pic"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="22dp"
        android:layout_marginBottom="20dp"
        android:background="@drawable/btn_share_bg_bg"
        android:gravity="center"
        android:text="分享"
        android:textColor="@color/white"
        android:textSize="18sp" />

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值