这是在我项目中遇到的一个效果,一个简单的Dialog。方便自己用,也给大家一个提示。
首先自定义一个类:PaperDialog
package com.dyh.drivingschool.ui.shop.customclass;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.dyh.drivingschool.R;
/**
* Created by ljg on 2018/5/7.
*/
public class PaperDialog extends Dialog {
private Context mContext;
private TextView tv_title;
private TextView tv_message;
private ImageView iv_cancel;
private Button tv_mDetermine;
private Button tv_mCancel;
super(context);
mContext = context;
}
public PaperDialog(Context context, int themeResId) {
super(context, themeResId);
mContext = context;
}
protected PaperDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public void createDialog(){
//获得inflater
LayoutInflater inflater = LayoutInflater.from(mContext);
//从xml转换为View
View layoutView = inflater.inflate(R.layout.alertdialog, null);
//PaperDialog将layoutView作为其View
addContentView(layoutView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));//View的大小
//标题
tv_title = (TextView) layoutView.findViewById(R.id.tv_dialog_title);
//内容
tv_message = (TextView) layoutView.findViewById(R.id.tv_dialog_message);
//图标
iv_cancel = (ImageView) layoutView.findViewById(R.id.iv_dialog_cancel);
//确认按钮
tv_mDetermine = (Button) layoutView.findViewById(R.id.bt_dialog_determine);
//取消按钮
tv_mCancel = (Button) layoutView.findViewById(R.id.bt_dialog_cancel);
}
public void setTitle(String title){
this.tv_title.setText(title);
}
public void setTitleSize(float size){
this.tv_title.setTextSize(size);
}
public void setMessage(CharSequence message) {
tv_message.setText(message);
}
public void setMessageSize(float size){
this.tv_message.setTextSize(size);
}
public void setCheck(CharSequence checkContent) {
tv_mDetermine.setText(checkContent);
}
public void setCheckSize(float size){
this.tv_mDetermine.setTextSize(size);
}
public void settv_mDetermineOnClickListener(String check, View.OnClickListener onClickListener){
this.tv_mDetermine.setText(check);
tv_mDetermine.setOnClickListener(onClickListener);
}
public void settv_mCancelOnClickListener(String check, View.OnClickListener onClickListener){
this.tv_mCancel.setText(check);
tv_mCancel.setOnClickListener(onClickListener);
}
}
接下来是布局:
<?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:background="@color/white"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
android:layout_marginBottom="@dimen/dp_20"
>
<TextView
android:id="@+id/tv_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系统提示"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
/>
<ImageView
android:id="@+id/iv_dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_alipay"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
/>
</RelativeLayout>
<View style="@style/Line.Horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="@dimen/dp_40"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨打电话 "
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/tv_dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="010-80665099 "
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="@dimen/dp_56"
>
<Button
android:id="@+id/bt_dialog_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"
/>
<Button
android:id="@+id/bt_dialog_determine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定" />
</LinearLayout>
</LinearLayout>
接下来就是Activity的调用:直接调用方法就好。
private void showlDialog() {
final PaperDialog paperDialog = new PaperDialog(CarShopDetailActivity.this, R.style.AlertDialogCustom);
paperDialog.createDialog();
//显示“标题”
paperDialog.setTitle("系统提示");
String msg = "010-80665099";
//显示内容
paperDialog.setMessage(msg);
paperDialog.setCanceledOnTouchOutside(true);
// paperDialog.setCancelable(false);
Window window = paperDialog.getWindow();
window.setBackgroundDrawableResource(R.drawable.shape_bu_yuanjiao);
//设置确认监听器
paperDialog.settv_mDetermineOnClickListener("确认", new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + 80665099));
startActivity(intent);
paperDialog.dismiss();
}
});
//设置取消监听器
paperDialog.settv_mCancelOnClickListener("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
paperDialog.dismiss();
}
});
//显示Dialog
paperDialog.show();
}
这是Style样式
<!--AlertDialog-->
<style name="AlertDialogCustom">
<item name="android:textColor">#ffffff</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">15sp</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
在这里用到了Shape圆角
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 圆角-->
<corners android:radius="15dp" />
<!--<padding-->
<!--android:bottom="5dp"-->
<!--android:left="5dp"-->
<!--android:top="5dp"-->
<!--android:right="5dp" />-->
<!--描边 他需要 2个参数,颜色 第二参数 宽度 -->
<!--<stroke android:color="#fff" android:width="1dp"/>-->
<!--填充背景色 -->
<!--<solid android:color="#fff" />-->
<!-- 渐变色 startColor 启示颜色 endColor最终颜色 type类型(如果类型为radial(径向渐变,一个圆心以半径的方式渐变),必须加上 android:gradientRadius="**")-->
<!--<gradient android:startColor="#456" android:endColor="#968" android:type="linear"/>-->
</shape>
就到这里一个简单的自定义Dialog就出来了,有什么不懂得可以问我。