通用对话框 和 样式
2016-01-28 17:09
597 查看
使用方式:
效果:
一个按钮
// 布局文件: R.layout.myalert_dialog.xml
// 代码实现:
// 其他 Dialog 样式
效果:
一个按钮
new MYAlertDialog(this, MYAlertDialog.TYPE_BUTTON_ONE, "提示", entity.getMessage(), "确定", "确定").show();二个按钮
new MYAlertDialog(this, MYAlertDialog.TYPE_BUTTON_TWO, title, content, leftbtn, rightbtn) { @Override public void clickCallBackLeft() { toActivity(InvestMoneyActivity.class, false); } }.show();
// 布局文件: R.layout.myalert_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/radio_dialog" android:gravity="center"> <TextView android:id="@+id/alert_dialog_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/padding_15" android:layout_marginRight="@dimen/padding_15" android:layout_marginTop="@dimen/padding_19" android:gravity="center" android:text="@string/prompt" android:textColor="@color/text_color_585858" android:textSize="@dimen/text_size_17" android:textStyle="bold" /> <TextView android:id="@+id/alert_dialog_message" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/alert_dialog_title" android:layout_centerInParent="true" android:layout_marginBottom="22dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:gravity="center" android:text="content" android:textColor="@color/text_color_585858" /> <View android:layout_width="fill_parent" android:layout_height="0.5dip" android:layout_below="@+id/alert_dialog_message" android:background="@color/line_d1d1d1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/alert_dialog_message"> <TextView android:id="@+id/alert_dialog_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="@string/text_btn_cancel" android:textColor="@color/color_037AFF" /> <View android:id="@+id/alert_dialog_line" android:layout_width="0.5dp" android:layout_height="fill_parent" android:background="@color/line_d1d1d1" /> <TextView android:id="@+id/alert_dialog_yes" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="@string/text_btn_ok" android:textColor="@color/color_037AFF" /> </LinearLayout> </RelativeLayout>
// 代码实现:
package com.hanya.financing.global.utils; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.Spanned; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import com.hanya.financing.R; /** * @author niu zhihua * @version V2.0 * @Description: 对话框 实现类 * @date 2015年12月29日 */ public class MYAlertDialog extends AlertDialog implements View.OnClickListener { // 对话框类型 public static final int TYPE_BUTTON_ONE = 2 << 1;//对话框上只显示一个按钮 public static final int TYPE_BUTTON_TWO = 2 << 2;//对话框上显示2个按钮 public static final int TYPE_CONTENT_LEFT = 2 << 3;//内容居左显示 public static final int TYPE_CONTENT_PARSE_AS_HTML = 2 << 4;//内容用html形式来解析 private TextView tv_message; private TextView tv_left; private TextView tv_right; private TextView tv_title; private View v_line; protected MYAlertDialog(Context context) { super(context); } int type = -1; String title; String content; String leftBtnText; String rightBtnText; Context context; /** * @param context * @param type 对话框类型:type :0 只显示2个按钮 type:1 显示一个按钮 */ public MYAlertDialog(Context context, int type, String title, String content, String leftBtnText, String rightBtnText) { super(context); this.context = context; this.type = type; this.title = title; this.content = content; this.leftBtnText = leftBtnText; this.rightBtnText = rightBtnText; } @Override public void show() { Activity a = (Activity) context; if (a != null && !a.isFinishing()) { super.show(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setBackgroundDrawableResource(android.R.color.transparent);//将dialog窗体的背景框设为透明 // 加载自定义布局 setContentView(R.layout.myalert_dialog); tv_title = (TextView) findViewById(R.id.alert_dialog_title); tv_message = (TextView) findViewById(R.id.alert_dialog_message); tv_left = (TextView) findViewById(R.id.alert_dialog_cancel); tv_right = (TextView) findViewById(R.id.alert_dialog_yes); v_line = findViewById(R.id.alert_dialog_line); // setDialogSize(300, 200); tv_left.setOnClickListener(this); tv_right.setOnClickListener(this); setCancelable(false); setTitle(title); setTextLeft(leftBtnText); setTextRight(rightBtnText); // 设置对话框类型 switch (type) { case TYPE_BUTTON_ONE: textLeftInGone(); setMessage(content); break; case TYPE_BUTTON_TWO: setMessage(content); break; case TYPE_BUTTON_ONE | TYPE_CONTENT_LEFT: tv_message.setGravity(Gravity.LEFT); setMessage(content); textLeftInGone(); case TYPE_BUTTON_TWO | TYPE_CONTENT_LEFT: tv_message.setGravity(Gravity.LEFT); setMessage(content); break; case TYPE_BUTTON_TWO | TYPE_CONTENT_LEFT | TYPE_CONTENT_PARSE_AS_HTML: tv_message.setGravity(Gravity.LEFT); try { Spanned c = android.text.Html.fromHtml(content); setMessage(c.toString()); } catch (Exception e) { e.printStackTrace(); } case TYPE_BUTTON_ONE | TYPE_CONTENT_LEFT | TYPE_CONTENT_PARSE_AS_HTML: tv_message.setGravity(Gravity.LEFT); try { Spanned c = android.text.Html.fromHtml(content); setMessage(c.toString()); } catch (Exception e) { e.printStackTrace(); } break; } } /** * 设置dialog内容 * * @param msg */ public void setMessage(String msg) { tv_message.setText(msg); } /** * 设置dialog标题 * * @param title */ public void setTitle(String title) { tv_title.setText(title); } /** * 设置左侧按钮文字 * * @param textLeft */ public void setTextLeft(String textLeft) { tv_left.setText(textLeft); } /** * 设置右侧按钮文字 * * @param textRight */ public void setTextRight(String textRight) { tv_right.setText(textRight); } /** * 只显示一个按钮 * 左侧按钮隐藏,右侧按钮居中 */ public void textLeftInGone() { tv_left.setVisibility(View.GONE); tv_right.setGravity(Gravity.CENTER_HORIZONTAL); v_line.setVisibility(View.GONE); } /** * 修改 框体大小 * * @param width * @param height */ public void setDialogSize(int width, int height) { WindowManager.LayoutParams params = getWindow().getAttributes(); params.width = width; params.height = height; this.getWindow().setAttributes(params); } public void clickCallBackLeft() { } //左侧按钮点击回调 public void clickCallBackRight() { } /** * 点击事件 */ @Override public void onClick(View v) { if (v.getId() == R.id.alert_dialog_yes) { clickCallBackRight(); } else if (v.getId() == R.id.alert_dialog_cancel) { clickCallBackLeft(); dismiss(); } if (isShowing()) { dismiss(); } } }
// 其他 Dialog 样式
<!--加载对话框--> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:backgroundDimEnabled">false</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
public Dialog createLoadingDialog(Context context) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载view LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局 // layout.getBackground().setAlpha(20); // main.xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.imgv_diaglog); // TextView tipTextView = (TextView) v.findViewById(R.id.txt_diaglog);// 提示文字 // 加载动画 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation); // 使用ImageView显示动画 spaceshipImage.startAnimation(hyperspaceJumpAnimation); // tipTextView.setText(msg);// 设置加载信息 // getWindow().setBackgroundDrawableResource(R.color.red);//将dialog窗体的背景框设为透明 Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog loadingDialog.setCancelable(true);// 不可以用“返回键”取消 loadingDialog.setCanceledOnTouchOutside(false);// 设置外部不点击 loadingDialog.getWindow().setBackgroundDrawableResource(R.drawable.bg_hui_normal);//将dialog窗体的背景框设为透明 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// 设置布局 return loadingDialog; }
相关文章推荐
- 精通CSS滤镜(filter)(实例解析)
- CSS视差滚动效果
- 移动端 h5开发相关内容总结——CSS篇
- css中的像素在iphone6 plus与iphone6渲染的不同效果
- CSS实时编辑显示
- 在EditText/TextView中插入表情图片、样式、下划线等
- CSS3 filter
- CSS浮动基础详解
- CSS 实现div宽度根据内容自适应
- 我就是要用CSS实现九宫格图
- css限制输入长度
- CSS书写规范及顺序
- css 3d
- CSS 文字溢出处添加省略号
- 检测CSS属性 是否支持
- css 属性通配符选择器
- CSS fliter
- CSS3下的147个颜色名称及对应颜色值
- css3伪元素
- Doc13(样式浮动及文本框空就变色)