效果圖:
前言:
最近在項目中遇到一個問題稼稿,就是啟動頁彈窗提示一個隱私政策的操作薄榛。我一開始用的是Dialog,但是為了符合公司的"審美"(裝B),所以為了使彈窗不那么單調(diào)让歼,我換成了PopupWindow,上線一段時間后敞恋,隨著用戶的增加,出現(xiàn)了問題谋右,讓我很是頭痛硬猫。原因是,在Android6.0之后的版本改执,彈窗都是正常居中顯示啸蜜,但是Android6.0之前(包含6.0)彈窗消失了。同時我給設(shè)置了彈窗出現(xiàn)時辈挂,點擊外部不能取消衬横,所以導(dǎo)致6.0的用戶在使用時,卡在了歡迎界面终蒂。網(wǎng)上找了很多博客蜂林,也沒有解決我的問題,所以后豫,又換回了DIalog悉尾。不同的是 為了美觀,實現(xiàn)一個自定義Dialog挫酿。
一构眯、創(chuàng)建自定義Dialog
1.創(chuàng)建一個類:CommonDialog并繼承Dialog。
下面附上我的代碼:
public class CommonDialog extends Dialog {
/**
? ? * 顯示的圖片
? ? */
? ? private ImageViewimageIv ;
? ? /**
? ? * 顯示的標(biāo)題
? ? */
? ? private TextViewtitleTv ;
? ? /**
? ? * 顯示的消息
? ? */
? ? private TextViewmessageTv ;
? ? /**
? ? * 確認(rèn)和取消按鈕
? ? */
? ? private ButtonnegtiveBn ,positiveBn;
? ? /**
? ? * 按鈕之間的分割線
? ? */
? ? private ViewcolumnLineView ;
? ? private TextViewprivacy;
? ? public CommonDialog(Context context) {
super(context, R.style.CustomDialog);
? ? }
/**
? ? * 都是內(nèi)容數(shù)據(jù)
? ? */
? ? private Stringmessage;
? ? private Stringtitle;
? ? private Stringpositive,negtive ;
? ? private int imageResId = -1 ;
? ? /**
? ? * 底部是否只有一個按鈕
? ? */
? ? private boolean isSingle =false;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
????????super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.base_layout_dialog);
? ? ? ? //按空白處不能取消動畫
? ? ? ? setCanceledOnTouchOutside(false);
? ? ? ? //初始化界面控件
? ? ? ? initView();
? ? ? ? //初始化界面數(shù)據(jù)
? ? ? ? refreshView();
? ? ? ? //初始化界面控件的事件
? ? ? ? initEvent();
? ? }
/**
? ? * 初始化界面的確定和取消監(jiān)聽器
? ? */
? ? private void initEvent() {
????????//設(shè)置確定按鈕被點擊后早龟,向外界提供監(jiān)聽
? ? ? ? positiveBn.setOnClickListener(new View.OnClickListener() {
????????????@Override
? ? ? ? ? ? public void onClick(View v) {
????????????????????if (onClickBottomListener!=null) {
????????????????????????onClickBottomListener.onPositiveClick();
? ? ? ? ? ? ? ? ????}
????????????}
????});
? ? ? ? //設(shè)置取消按鈕被點擊后惫霸,向外界提供監(jiān)聽
? ? ? ? negtiveBn.setOnClickListener(new View.OnClickListener() {
????????????@Override
? ? ? ? ? ? public void onClick(View v) {
????????????????????if (onClickBottomListener!=null) {
????????????????????????????onClickBottomListener.onNegtiveClick();
? ? ? ? ? ? ? ????? }
? ? ? ? ? ? }
????});
? ? ? ? //設(shè)置隱私政策被點擊后,向外界提供監(jiān)聽
? ? ? ? privacy.setOnClickListener(new View.OnClickListener() {
????????????@Override
? ? ? ? ? ? public void onClick(View view) {
????????????????if(onClickBottomListener!=null){
????????????????????????onClickBottomListener.onPrivacyClick();
? ? ? ? ? ? ? ? ????}
????????????}
????});
}
/**
? ? * 初始化界面控件的顯示數(shù)據(jù)
? ? */
? ? private void refreshView() {
//如果用戶自定了title和message
? ? ? ? if (!TextUtils.isEmpty(title)) {
????????????????titleTv.setText(title);
? ? ? ? ? ? ????titleTv.setVisibility(View.VISIBLE);
? ? ? ? }else {
????????????????titleTv.setVisibility(View.GONE);
? ? ? ? }
????????if (!TextUtils.isEmpty(message)) {
????????????????messageTv.setText(message);
? ? ? ? }
????????//如果設(shè)置按鈕的文字
? ? ? ? if (!TextUtils.isEmpty(positive)) {
????????????????positiveBn.setText(positive);
? ? ? ? }else {
????????????????positiveBn.setText("確定");
? ? ? ? }
????????if (!TextUtils.isEmpty(negtive)) {
????????????????negtiveBn.setText(negtive);
? ? ? ? }else {
????????????????negtiveBn.setText("取消");
? ? ? ? }
????????if (imageResId!=-1){
i????????????????mageIv.setImageResource(imageResId);
? ? ? ? ? ? ????imageIv.setVisibility(View.VISIBLE);
? ? ? ? }else {
????????????????imageIv.setVisibility(View.GONE);
? ? ? ? }
/**
? ? ? ? * 只顯示一個按鈕的時候隱藏取消按鈕葱弟,回掉只執(zhí)行確定的事件
? ? ? ? */
? ? ? ? if (isSingle){
? ? ? ? ? ? columnLineView.setVisibility(View.GONE);
? ? ? ? ? ? negtiveBn.setVisibility(View.GONE);
? ? ? ? }else {
????????????negtiveBn.setVisibility(View.VISIBLE);
? ? ? ? ? ? columnLineView.setVisibility(View.VISIBLE);
? ? ? ? }
}
????@Override
? ? public void show() {
????????????super.show();
? ? ? ? ????refreshView();
? ? ????}
/**
? ? * 初始化界面控件
? ? */
? ? private void initView() {
????????negtiveBn = (Button) findViewById(R.id.negtive);
? ? ? ? positiveBn = (Button) findViewById(R.id.positive);
? ? ? ? titleTv = (TextView) findViewById(R.id.title);
? ? ? ? messageTv = (TextView) findViewById(R.id.message);
? ? ? ? imageIv = (ImageView) findViewById(R.id.image);
? ? ? ? columnLineView = findViewById(R.id.column_line);
? ? ? ? privacy = findViewById(R.id.privacy);
? ? }
/**
? ? * 設(shè)置確定取消按鈕的回調(diào)
? ? */
? ? public OnClickBottomListeneronClickBottomListener;
? ? public CommonDialogsetOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
????????????this.onClickBottomListener = onClickBottomListener;
????????????return this;
? ? }
public interface OnClickBottomListener{
/**
? ? ? ? * 點擊確定按鈕事件
? ? ? ? */
? ? ? ? public void onPositiveClick();
? ? ? ? /**
? ? ? ? * 點擊取消按鈕事件
? ? ? ? */
? ? ? ? public void onNegtiveClick();
? ? ? ? /**
? ? ? ? * 點擊隱私政策事件
? ? ? ? */
? ? ? ? public void onPrivacyClick();
? ? }
public StringgetMessage() {
return message;
? ? }
public CommonDialogsetMessage(String message) {
this.message = message;
return this ;
? ? }
public StringgetTitle() {
return title;
? ? }
public CommonDialogsetTitle(String title) {
this.title = title;
return this ;
? ? }
public StringgetPositive() {
return positive;
? ? }
public CommonDialogsetPositive(String positive) {
this.positive = positive;
return this ;
? ? }
public StringgetNegtive() {
return negtive;
? ? }
public CommonDialogsetNegtive(String negtive) {
this.negtive = negtive;
return this ;
? ? }
public int getImageResId() {
return imageResId;
? ? }
public boolean isSingle() {
return isSingle;
? ? }
public CommonDialogsetSingle(boolean single) {
isSingle = single;
return this ;
? ? }
public CommonDialogsetImageResId(int imageResId) {
this.imageResId = imageResId;
return this ;
? ? }
}
上面的代碼自己看看 然后你就可以直接復(fù)制拿去用了壹店。
然后補(bǔ)充一個style文件 和xml文件
style:
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:orientation="vertical"
? ? android:layout_centerInParent="true"
? ? android:paddingTop="16dp"
? ? android:background="@drawable/base_dialog_bg"
? ? xmlns:tools="http://schemas.android.com/tools">
? ? ? ? android:id="@+id/title"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_gravity="center"
? ? ? ? android:layout_marginLeft="16dp"
? ? ? ? android:layout_marginRight="16dp"
? ? ? ? android:layout_marginBottom="16dp"
? ? ? ? android:gravity="center"
? ? ? ? tools:text="消息提示"
? ? ? ? android:visibility="visible"
? ? ? ? android:textColor="#333333"
? ? ? ? android:textSize="18sp" />
? ? ? ? android:id="@+id/image"
? ? ? ? tools:src="@mipmap/ic_launcher"
? ? ? ? android:layout_gravity="center"
? ? ? ? android:maxHeight="150dp"
? ? ? ? android:maxWidth="150dp"
? ? ? ? android:layout_marginBottom="10dp"
? ? ? ? android:layout_marginLeft="16dp"
? ? ? ? android:layout_marginRight="16dp"
? ? ? ? android:visibility="visible"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content" />
? ? ? ? android:layout_gravity="center"
? ? ? ? android:layout_width="300dp"
? ? ? ? android:layout_height="400dp">
? ? ? ? ? ? android:id="@+id/message"
? ? ? ? ? ? android:layout_width="fill_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:gravity="center|left"
? ? ? ? ? ? android:layout_marginLeft="20dp"
? ? ? ? ? ? android:layout_marginRight="20dp"
? ? ? ? ? ? android:lineSpacingExtra="3dp"
? ? ? ? ? ? android:lineSpacingMultiplier="1.2"
? ? ? ? ? ? android:textSize="14dp"
? ? ? ? ? ? android:textColor="#999999"
? ? ? ? ? ? tools:text="提示消息" />
? ? ? ? android:id="@+id/privacy"
? ? ? ? android:textSize="@dimen/dp_14"
? ? ? ? android:textColor="#38ADFF"
? ? ? ? android:layout_marginTop="@dimen/dp_16"
? ? ? ? android:layout_gravity="center"
? ? ? ? android:text="隱私政策"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"/>
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="1px"
? ? ? ? android:layout_marginTop="16dp"
? ? ? ? android:background="#E4E4E4" />
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? android:id="@+id/negtive"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginLeft="10dp"
? ? ? ? ? ? android:paddingTop="16dp"
? ? ? ? ? ? android:paddingBottom="16dp"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:background="@null"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:singleLine="true"
? ? ? ? ? ? tools:text="No"
? ? ? ? ? ? android:textColor="#999999"
? ? ? ? ? ? android:textSize="16sp" />
? ? ? ? ? ? android:id="@+id/column_line"
? ? ? ? ? ? android:layout_width="1px"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:background="#E4E4E4" />
? ? ? ? ? ? android:id="@+id/positive"
? ? ? ? ? ? android:layout_width="0dp"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_marginRight="10dp"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:paddingTop="16dp"
? ? ? ? ? ? android:paddingBottom="16dp"
? ? ? ? ? ? android:background="@null"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:singleLine="true"
? ? ? ? ? ? tools:text="Yes"
? ? ? ? ? ? android:textColor="#38ADFF"
? ? ? ? ? ? android:textSize="16sp" />
</LinearLayout>
以上為止,我們所需的東西就全部湊齊了芝加。接下來看用法:
在我們需要彈窗的Activity或者fragment中創(chuàng)建一個方法:initDialog();
方法中硅卢,new一個我們剛創(chuàng)建的Dialog類。
CommonDialog commonDialog =new CommonDialog(上下文對象);
commonDialog .setTitle("標(biāo)題")
? ? ? ? ? ? ? ? ? ? ? ? ? .setMessage("提示內(nèi)容")
? ? ? ? ? ? ? ? ? ? ? ? ? .setSingle(true/false)//false為雙選,true為單選将塑÷龆伲看自己需求
? ? ? ? ? ? ? ? ? ? ? ? ? .setOnClickBottomListener(new CommonDialog.OnClickBottomListener()){
? ??????????????????????????????@Override
????????????????????????????????????public void onPositiveClick() {
? ? ????????????????????????????????startActivity(MainActivity.class);//做你想做的事。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();
????????????????????????????????????}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onNegtiveClick() {
????????????????????????????????//退出程序
? ? ????????????????????????????System.exit(0);
????????????????????????????}
? ? ? ? ? ? ? ? ? ?}).show();
????????//當(dāng)Dialog出現(xiàn)時点寥,返回鍵失效
????????commonDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
????????????@Override
? ? ????????public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
????????????????????if(keyEvent.getKeyCode()==KeyEvent.KEYCODE_BACK){
????????????????????????????return true;
? ? ? ? ????????????}
????????????????????????????return false;
? ? ????????????}
????????});
好了艾疟,到這里,一個簡單的自定義Dialog就完成了敢辩,趕緊拿去試試吧蔽莱。
注意:具體需求,直接改動就可以戚长。代碼簡單也好懂盗冷。