最近写了一个弹框,发现之前写弹窗方式。
- dialog
- 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>