一、前提
自定义的Dialog在很多地方都用得上,因为不管是游戏还是其他app在效果上肯定不会局限于android studio中所给的dialog,所以很多app都实现自己的dialog
二、目标
实现一个自定义的dialog
三、内容
1、首先在activity_main中添加一个按钮
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text"
android:textAllCaps="false"
/>
</LinearLayout>
2、在自定义的xml文件中布局自定义的dialog
<?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="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:text="提示"
android:textStyle="bold"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:text="是否删除?"
android:layout_marginTop="20dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="#0FA8CE"
android:textSize="20sp"
/>
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#000000"/>
<TextView
android:id="@+id/tv4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:textColor="#0FA8CE"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
3、新建一个java类,在内部实现方法,因为都是之前所提到的方法,在这里不做过多解释,其中有一段设置宽度的代码,是固定代码可直接复制去用
package com.example.sunny.dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
/**
* Created by Sunny on 2020/4/21.
*/
public class Dialog extends android.app.Dialog implements View.OnClickListener{
private TextView textView1,textView2,textView3,textView4;
private String title,message,cancel,confirm;
private OnCancelListener cancelListener;
private OnConfirmListener confirmListener;
public Dialog(Context context) {
super(context);
}
public Dialog(Context context, int themeResId) {
super(context, themeResId);
}
public void setTitle(String title) {
this.title = title;
}
public void setMessage(String message) {
this.message = message;
}
public void setCancel(String cancel,OnCancelListener listener) {
this.cancel = cancel;
this.cancelListener=listener;
}
public void setConfirm(String confirm,OnConfirmListener listener) {
this.confirm = confirm;
this.confirmListener=listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
//设置宽度,固定代码
WindowManager m=getWindow().getWindowManager();
Display d=m.getDefaultDisplay();
WindowManager.LayoutParams p=getWindow().getAttributes();
Point size=new Point();
d.getSize(size);
p.width=(int)(size.x*0.8);//设置dialog的宽度为当前手机屏幕宽度*0.8
getWindow().setAttributes(p);
textView1= (TextView) findViewById(R.id.tv1);
textView2= (TextView) findViewById(R.id.tv2);
textView3= (TextView) findViewById(R.id.tv3);
textView4= (TextView) findViewById(R.id.tv4);
if(!TextUtils.isEmpty(title)){//不为空
textView1.setText(title);
}
if(!TextUtils.isEmpty(message)){//不为空
textView2.setText(message);
}
if(!TextUtils.isEmpty(cancel)){//不为空
textView3.setText(cancel);
}
if(!TextUtils.isEmpty(confirm)){//不为空
textView4.setText(confirm);
}
textView3.setOnClickListener(this);
textView4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv3:
if(cancelListener!=null){
cancelListener.onCancel(this);
}
dismiss();
break;
case R.id.tv4:
if(confirmListener!=null){
confirmListener.onConfirm(this);
}
dismiss();
break;
}
}
public interface OnCancelListener{
void onCancel(Dialog dialog);
}
public interface OnConfirmListener{
void onConfirm(Dialog dialog);
}
}
4、在MainActivity中添加
package com.example.sunny.dialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setTitle("提示");
dialog.setMessage("确认取消?");
dialog.setCancel("取消", new Dialog.OnCancelListener() {
@Override
public void onCancel(Dialog dialog) {
ToastUtil.showMsg(MainActivity.this,"cancel");
}
});
dialog.setConfirm("确认", new Dialog.OnConfirmListener() {
@Override
public void onConfirm(Dialog dialog) {
ToastUtil.showMsg(MainActivity.this,"ok");
}
});
dialog.show();
}
});
}
}
四、总结
自定义的Dialog可任意实现,根据自己的创作来完成