自定义Dialog
来源:互联网 发布:淘宝客服用语 物流慢 编辑:程序博客网 时间:2024/06/05 09:28
自定义Dialog
在工作中会发现,系统的dialog已经不能满足我们的需求了,我们想要自己设计的美观的各种样式,那么自定义样式的dialog就灰常重要了,我的效果如下
自定义样式dialog的步骤概要如下
##
- 先自定义一个弹出框的样式
- 自己实现CustomDialog类,继承自Dialog,实现里面方法,在里面加载自定义样式的弹出框;
- 使用时,与使用Dialog一样
##**
代码实现
**
CustomDialog的布局代码
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:orientation="vertical" android:padding="20.0dip" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/tanchuang" android:orientation="vertical" > <TextView android:id="@+id/title" android:textSize="15dp" android:textColor="#333333" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="27dp" android:text="标题" android:visibility="visible" /> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp" android:gravity="center_horizontal" > <TextView android:id="@+id/message" android:textColor="#333333" android:textSize="12dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left|center" android:lineSpacingMultiplier="1.5" android:text="sssssdddddddddddddddddddddddddddddddddddddddddddddfvawwwwwwwwwwwww" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="60.0dip" android:layout_gravity="bottom" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/positiveButton" android:textColor="#ffffff" android:textSize="12dp" android:layout_width="114.0dip" android:layout_height="40.0dip" android:gravity="center" android:background="@drawable/bt_bg_press_fen" android:text="确定" /> <Button android:id="@+id/negativeButton" android:textColor="#333333" android:textSize="12dp" android:background="@drawable/bt_bg_press_fen" android:layout_width="114.0dip" android:layout_height="40.0dip" android:layout_marginLeft="20.0dip" android:gravity="center" android:text="取消" /> </LinearLayout> </LinearLayout></FrameLayout>
**
自定义实现
**
package viewlimaomin;import com.example.lmmdialog.R;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class CustomDialog extends Dialog { public CustomDialog(Context context) { super(context); } public CustomDialog(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private Boolean Outside; private String message; private String positiveButtonText; private String negativeButtonText; private View contentView; private DialogInterface.OnClickListener positiveButtonClickListener; private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.context = context; } public Builder setMessage(String message) { this.message = message; return this; } /** * Set the Dialog message from resource * * @param title * @return */ public Builder setMessage(int message) { this.message = (String) context.getText(message); return this; } /** * Set the Dialog title from resource * * @param title * @return */ public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } /** * Set the Dialog title from String * * @param title * @return */ public Builder setContentView(View v) { this.contentView = v; return this; } /** * Set the positive button resource and it's listener * * @param positiveButtonText * @return */ public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = (String) context .getText(positiveButtonText); this.positiveButtonClickListener = listener; return this; } public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) { this.positiveButtonText = positiveButtonText; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = (String) context .getText(negativeButtonText); this.negativeButtonClickListener = listener; return this; } public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) { this.negativeButtonText = negativeButtonText; this.negativeButtonClickListener = listener; return this; } public CustomDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // instantiate the dialog with the custom Theme final CustomDialog dialog = new CustomDialog(context,R.style.Dialog); View layout = inflater.inflate(R.layout.dialog_limaomin_layout, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); dialog.setCanceledOnTouchOutside(Outside); // set the dialog title ((TextView) layout.findViewById(R.id.title)).setText(title); // set the confirm button if (positiveButtonText != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setText(positiveButtonText); if (positiveButtonClickListener != null) { ((Button) layout.findViewById(R.id.positiveButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.positiveButton).setVisibility( View.GONE); } // set the cancel button if (negativeButtonText != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setText(negativeButtonText); if (negativeButtonClickListener != null) { ((Button) layout.findViewById(R.id.negativeButton)) .setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { // if no confirm button just set the visibility to GONE layout.findViewById(R.id.negativeButton).setVisibility( View.GONE); } // set the content message if (message != null) { ((TextView) layout.findViewById(R.id.message)).setText(message); } else if (contentView != null) { // if no message set // add the contentView to the dialog body ((LinearLayout) layout.findViewById(R.id.content)) .removeAllViews(); ((LinearLayout) layout.findViewById(R.id.content)).addView( contentView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } dialog.setContentView(layout); return dialog; } }}```**<div class="se-preview-section-delimiter"></div>使用--<div class="se-preview-section-delimiter"></div>
viewlimaomin.CustomDialog.Builder builder = new viewlimaomin.CustomDialog.Builder(MainActivity.this);
// builder.setCanceledOnTouchOutside(true);
builder.setMessage(“自定义的内容”);
builder.setTitle(“提示”);
builder.setOutside(true);
builder.setPositiveButton(“确定”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
} }); builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }
“`
转载请注明作者链接
点击进原创博客
下载源码
- 自定义Dialog---继承Dialog
- 自定义Dialog android Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义dialog
- 自定义 Dialog
- 自定义dialog
- 自定义 dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义Dialog
- hdu 5969 最大的位或
- 【第九课】包---异常的基础
- 读取Spring配置文件
- 解决Android Studio配置签名出现的问题
- 两次入坑逆向拓扑序(POJ 3687 Labeling Balls and HDU 4857 逃生)
- 自定义Dialog
- 理解async
- leetcode63. Unique Paths II
- 5.1.1图的深度优先遍历
- 如何防止表单重复提交
- Qt 中设置窗体透明度的几种方法
- 在 JavaScript 中 prototype 和 __proto__ 有什么区别
- 终于知道为什么我的ButterKnife不管用了
- 状压DP进阶-POJ2411