问题描述
- AlertDialog中的按钮能不能添加图标
-
我做的个提示框 我想把按钮换成带图标的不知道可以不。如果可以希望能给我个例子
解决方案
解决方案二:
要实现这个功能的话,只有自定义AlertDialog的布局了,然后用builder.setView()放上去,系统的按钮是不能改的
解决方案三:
那你就自定义一个dialog啊。
解决方案四:
可以,setDrawable
解决方案五:
自定义dialog,
package com.antex.assist;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.antex.R;
/**
* <h3>package:</h3> com.antex.assist <br>
* <h3>file:</h3> MyDialog.java <br>
*
* <h3>Description:自定义dialog</h3><br>
* <h3>创建时间:</h3>2013-10
*
*
* <pre>
* @author xiaosanyu<br>
* email: 446251495@qq.com<br>
* blog: <a href="http://blog.csdn.net/a87b01c14">http://blog.csdn.net/a87b01c14</a>
* </pre>
*
*/
public class MyDialog extends Dialog {
/**
* 构造函数
*
* @param context
* 上下文环境
* @param theme
* 主题
*/
public MyDialog(Context context, int theme) {
super(context, theme);
}
/**
* 构造函数
*
* @param context
* 上下文环境
*/
public MyDialog(Context context) {
super(context);
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder {
/** 上下文环境 */
private Context context;
/** 标题 */
private String title;
/** 消息内容 */
private String message;
/** 左侧按钮文字 */
private String positiveButtonText;
/** 右侧按钮文字 */
private String negativeButtonText;
/** 中间按钮文字 */
private String neutralButtonText;
/** 子视图 */
private View contentView;
/** 标题图标 */
private int titleimg = 0;
/** 左侧按钮点击监听 */
private DialogInterface.OnClickListener positiveButtonClickListener;
/** 中间按钮点击监听 */
private DialogInterface.OnClickListener neutralButtonClickListener;
/** 右侧按钮点击监听 */
private DialogInterface.OnClickListener negativeButtonClickListener;
/**
* 构造器
*
* @param context
* 上下文环境
*/
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
*
* @param message
* String类型对话框消息
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param message
* int型根据ID获取会话框消息
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* int型根据ID获取对话框标题
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog titleimg from resource
*
* @param titleimg
* 对话框图标
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setIcon(int titleimg) {
this.titleimg = titleimg;
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* String型对话框标题
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog. If a message is set, the
* contentView is not added to the Dialog...
*
* @param v
* 对话框子视图
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param positiveButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
*
* @param positiveButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
*
* @param negativeButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
*
* @param negativeButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the Neutral button resource and it's listener
*
* @param neutralButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNeutralButton(int neutralButtonText,
DialogInterface.OnClickListener listener) {
this.neutralButtonText = (String) context
.getText(neutralButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the Neutral button text and it's listener
*
* @param neutralButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNeutralButton(String neutralButtonText,
DialogInterface.OnClickListener listener) {
this.neutralButtonText = neutralButtonText;
this.neutralButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*
* @return MyDialog 返回自定义Dialog
*/
public MyDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final MyDialog dialog = new MyDialog(context, R.style.MyDialog);
View layout = inflater.inflate(R.layout.dialog, new LinearLayout(
context), false);
ImageView titleimage = (ImageView) layout
.findViewById(R.id.dialog_title_image);
TextView tv = (TextView) layout.findViewById(R.id.dialog_msg);
Button positiveButton = (Button) layout
.findViewById(R.id.positiveButton);
Button negativeButton = (Button) layout
.findViewById(R.id.negativeButton);
Button neutralButton = (Button) layout
.findViewById(R.id.neutralButton);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
if (title != null)
((TextView) layout.findViewById(R.id.dialog_title))
.setText(title);
if (titleimg != 0)
titleimage.setBackgroundResource(titleimg);
else
titleimage.setVisibility(View.GONE);
// set the confirm button
if (positiveButtonText != null) {
positiveButton.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
positiveButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
positiveButton.setVisibility(View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
negativeButton.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
negativeButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
negativeButton.setVisibility(View.GONE);
}
// set the neutral button
if (neutralButtonText != null) {
neutralButton.setText(neutralButtonText);
LayoutParams lp = positiveButton.getLayoutParams();
lp.width = convertDIP2PX(80);
positiveButton.setLayoutParams(lp);
RelativeLayout.LayoutParams contentLayoutParams = new RelativeLayout.LayoutParams(
convertDIP2PX(80),
RelativeLayout.LayoutParams.WRAP_CONTENT);
contentLayoutParams.leftMargin = convertDIP2PX(15);
contentLayoutParams.addRule(RelativeLayout.RIGHT_OF,
R.id.neutralButton);
negativeButton.setLayoutParams(contentLayoutParams);
if (neutralButtonClickListener != null) {
neutralButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
neutralButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no neutral button just set the visibility to GONE
neutralButton.setVisibility(View.GONE);
}
// set the content message
if (message != null) {
tv.setText(message);
}
if (contentView != null) {
// add the contentView to the dialog body
if (message == null || message == "")
tv.setVisibility(View.GONE);
LinearLayout ll = (LinearLayout) layout
.findViewById(R.id.content);
// WindowManager manager = (WindowManager) context
// .getSystemService(Context.WINDOW_SERVICE);
// Display display = manager.getDefaultDisplay();
// int width = display.getWidth();
LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
contentLayoutParams.weight = 0;
// contentLayoutParams.gravity=Gravity.CENTER_VERTICAL;
contentLayoutParams.leftMargin = 30;
contentLayoutParams.rightMargin = 30;
contentLayoutParams.topMargin = 10;
ll.addView(contentView, contentLayoutParams);
}
dialog.setContentView(layout);
return dialog;
}
/**
* 转换dip为px
*
* @param dip
* dip大小
* @return int dip单位转换成px大小
*/
public int convertDIP2PX(int dip) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}
}
}
时间: 2024-11-03 21:55:16