如題,當(dāng)你覺得系統(tǒng)給的對話框太丑了,想自己寫時(shí)發(fā)現(xiàn)完全不知道怎么寫,問度娘,又看得一頭霧水,那今天就教你傻瓜式的用別人寫好的吧,怎么實(shí)現(xiàn)的我不管,只要能用就行了
先上圖,你覺得OK就用,不Ok就pass:


第一步:
新建文件:

新建7個(gè)resource文件,文件名,這個(gè)很重要,分別叫:
第一個(gè):background_shape.xml
第一個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="35dp"/>
<!--設(shè)置背景顏色-->
<solid android:color="#f0f0f0f0"/>
</shape>
第二個(gè):left_button_shape.xml
第二個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/left_button_shape_depend_resource" android:state_pressed="true"/>
<!--<item android:drawable="@drawable/trans_bg"/>-->
</selector>
第三個(gè):left_button_shape_depend_resource.xml ?
第三個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:bottomLeftRadius="35dp"/>
<!--設(shè)置按鈕點(diǎn)擊時(shí)的顏色-->
<solid android:color="#f0d3d3d3"/>
</shape>
第四個(gè):right_button_shape.xml
第四個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/right_button_shape_depend_resource" android:state_pressed="true"/>
<!--<item android:drawable="@drawable/trans_bg"/>-->
</selector>
第五個(gè): right_button_shape_depend_resource.xml
第五個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:bottomRightRadius="35dp"/>
<!--設(shè)置按鈕點(diǎn)擊時(shí)的顏色-->
<solid android:color="#f0d3d3d3"/>
</shape>
第六個(gè):single_button_shape.xml
第六個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/single_button_shape_depend_resource" android:state_pressed="true"/>
<!--<item android:drawable="@drawable/trans_bg"/>-->
</selector>
第七個(gè):single_button_shape_depend_resource.xml
第七個(gè)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:bottomLeftRadius="35dp"
android:bottomRightRadius="35dp"/>
<solid android:color="#f0d3d3d3"/>
</shape>
接下來----------------------------------------------------------------------------------------------
還要?jiǎng)?chuàng)建最后一個(gè)xml文件,在values下面創(chuàng)建,如圖:

名稱還是要注意,叫:custom_style.xml
其代碼如下 :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<!--背景顏色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除標(biāo)題 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除邊框-->
<item name="android:windowFrame">@null</item>
<!--是否浮現(xiàn)在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
別問我可不可以起其他名,如果你是大佬隨意,是小白還是老老實(shí)實(shí),可以避免很對問題,聽話不挨打
第二步:
新建一個(gè)Java class,如圖:

新建一個(gè)類,類名為:MyDialog,將一下代碼復(fù)制:
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 使用說明:要想改對話框圓角的大小去background_shape.xml里面改,
* 同時(shí)還要到left_button_shape_depend_resource.xml和right_button_shape_depend_resource.xml
* 里面去改,要想改背景顏色也是一樣的
*
* 本類提供方法setTitleSize()改標(biāo)題字號和setMessageSize()改內(nèi)容字號
* setLeft() setRight() setTop() setBottom()四個(gè)方法改內(nèi)容上下左右邊距
* setTitleHeight() setBottomHeight()分別設(shè)置標(biāo)題和底部按鈕的高度
* 其他的想改字體顏色的到代碼里面去看就行了 改標(biāo)題顏色定位到131行代碼
* 內(nèi)容142 按鈕158和171,自行修改,方法就懶得提供了
* 本類一共加載了四次資源,一次是主題,一次是背景,兩次是按鈕,左一個(gè),右一個(gè)
*/
public class MyDialog extends Dialog{
private String title = "提示";
private String message = "這是一個(gè)彈窗";
private Context context;
private boolean isThereNavigateButton = false;
private boolean isTherePositiveButton = false;
private float titleSize = 22;
private float messageSize = 18;
private int left = 75;
private int right = 75;
private int top = 0;
private int bottom = 80;
private int titleHeight = 150;
private int bottomHeight = 150;
private TextView titleTextView;
private TextView messageTextView;
private Button navigateButton;
private Button positiveButton;
private LinearLayout linearLayout;
private ImageView imageView1;
private ImageView imageView2;
private LinearLayout linearLayout1;
public MyDialog(Context context) {
super(context, R.style.CustomDialog);//加載資源
this.context = context;
float scale = context.getResources().getDisplayMetrics().density;
left = (int) (25*scale);//設(shè)置dp值
right = (int) (25*scale);//設(shè)置dp值
createAllView();
setLayout();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加載布局
setContentView(linearLayout);
//設(shè)置按鈕有無
if (!isTherePositiveButton){
positiveButton.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
navigateButton.setBackgroundResource(R.drawable.single_button_shape);
}
if (!isThereNavigateButton){
navigateButton.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
positiveButton.setBackgroundResource(R.drawable.single_button_shape);
}
if (!isThereNavigateButton && !isTherePositiveButton){
imageView1.setVisibility(View.GONE);
linearLayout1.setVisibility(View.GONE);
}
}
public MyDialog setNavigateButton(String content, final View.OnClickListener listener) {
navigateButton.setText(content);
navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dismiss();
}
});
isThereNavigateButton = true;
return this;
}
public MyDialog setPositiveButton(String content, final View.OnClickListener listener) {
positiveButton.setText(content);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dismiss();
}
});
isTherePositiveButton = true;
return this;
}
//show方法
public void show(){
super.show();
}
//創(chuàng)建控件并設(shè)置控件的參數(shù)
private void createAllView(){
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setBackgroundResource(R.drawable.background_shape);//加載資源
titleTextView = new TextView(context);
titleTextView.setGravity(Gravity.CENTER);
titleTextView.setText("警告");
titleTextView.setPadding(0,50,0,0);
titleTextView.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
titleTextView.setHeight(titleHeight);//設(shè)置標(biāo)題的寬度
titleTextView.setTextColor(Color.BLACK);
titleTextView.setTextSize(titleSize);//設(shè)置標(biāo)題字體大小
titleTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//設(shè)置字體樣式
messageTextView = new TextView(context);
messageTextView.setGravity(Gravity.CENTER);
messageTextView.setTypeface(Typeface.SANS_SERIF);
messageTextView.setText("這是內(nèi)容");
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
messageTextView.setLayoutParams(params3);
messageTextView.setTextColor(Color.BLACK);
messageTextView.setTextSize(messageSize);//設(shè)置內(nèi)容的字體大小
messageTextView.setPadding(left, top, right, bottom);//設(shè)置內(nèi)容與四周的間距
imageView1 = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 4);
imageView1.setLayoutParams(params);
imageView1.setBackgroundColor(0xffb1b2b2);//設(shè)置背景顏色
linearLayout1 = new LinearLayout(context);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomHeight);
linearLayout1.setLayoutParams(params1);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
positiveButton = new Button(context);
positiveButton.setTextColor(0xff3478f6);//設(shè)置字體顏色
positiveButton.setText("確定");
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
positiveButton.setLayoutParams(params4);
positiveButton.setTextSize(20);//設(shè)置確定的字體大小
positiveButton.setBackgroundResource(R.drawable.left_button_shape);//加載資源
imageView2 = new ImageView(context);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(4, LinearLayout.LayoutParams.MATCH_PARENT);
imageView2.setLayoutParams(params2);
imageView2.setBackgroundColor(0xffb1b2b2);
navigateButton = new Button(context);
navigateButton.setTextColor(0xff3478f6);
navigateButton.setText("取消");
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,1);
navigateButton.setLayoutParams(params5);
navigateButton.setTextSize(20);//設(shè)置取消的字體大小
navigateButton.setBackgroundResource(R.drawable.right_button_shape);//加載資源
}
//添加布局
private void setLayout(){
linearLayout.addView(titleTextView);
linearLayout.addView(messageTextView);
linearLayout.addView(imageView1);
linearLayout.addView(linearLayout1);
linearLayout1.addView(positiveButton);
linearLayout1.addView(imageView2);
linearLayout1.addView(navigateButton);
}
public String getTitle() {
return title;
}
//設(shè)置標(biāo)題
public MyDialog setTitle(String title) {
this.title = title;
if (titleTextView == null){
System.out.println("null");
}else titleTextView.setText(title);
return this;
}
public String getMessage() {
return message;
}
//設(shè)置內(nèi)容
public MyDialog setMessage(String message) {
this.message = message;
messageTextView.setText(message);
return this;
}
//設(shè)置標(biāo)題大小
public void setTitleSize(float titleSize) {
this.titleSize = titleSize;
titleTextView.setTextSize(titleSize);
}
//設(shè)置內(nèi)容大小
public void setMessageSize(float messageSize) {
this.messageSize = messageSize;
messageTextView.setTextSize(messageSize);
}
//設(shè)置內(nèi)容左間距
public void setLeft(int left) {
this.left = left;
messageTextView.setPadding(left, top, right, bottom);//設(shè)置內(nèi)容與四周的間距
}
//設(shè)置內(nèi)容右間距
public void setRight(int right) {
this.right = right;
messageTextView.setPadding(left, top, right, bottom);//設(shè)置內(nèi)容與四周的間距
}
//設(shè)置內(nèi)容上間距
public void setTop(int top) {
this.top = top;
messageTextView.setPadding(left, top, right, bottom);//設(shè)置內(nèi)容與四周的間距
}
//設(shè)置內(nèi)容下間距
public void setBottom(int bottom) {
this.bottom = bottom;
messageTextView.setPadding(left, top, right, bottom);//設(shè)置內(nèi)容與四周的間距
}
//設(shè)置底部按鈕的高度
public void setBottomHeight(int bottomHeight) {
this.bottomHeight = bottomHeight;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomHeight);
linearLayout1.setLayoutParams(params);
}
//設(shè)置頂部標(biāo)題的高度
public void setTitleHeight(int titleHeight) {
this.titleHeight = titleHeight;
titleTextView.setHeight(titleHeight);
}
}
第三步:
使用:
使用方法和Android自帶的AlertDialog是一樣的,只是不需要create,直接設(shè)置完標(biāo)題和內(nèi)容,確定和取消,就show就行了,下面是示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyDialog(MainActivity.this)
.setTitle("警告")
.setMessage("你確定要?jiǎng)h除該文件嗎?")
.setPositiveButton("是", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.setPositiveButton("否", new View.OnClickListener() {
@Override
public void onClick(View v) {
}
})
.show();
}