AlertDialog中修改字体颜色。AlertDialog简单使用,小米2max无颜色

 
一、一般的dialog
(在有的Android 
AlertDialog dialog=new AlertDialog.Builder(mContext)不能构建成功
);

	需要先得到Builder对象:

AlertDialog.Builder build= new AlertDialog.Builder(mContext);
	build.setTitle("提示");
	build
.setMessage("您还未绑定支付宝?");


build
.setNegativeButton("考虑一下", null);

build
.setPositiveButton("前去绑定", null);

	
build
.show();

二、小米2max,确定和取消没有颜色的问题,或者设置Button的颜色?
	
AlertDialog.Builder build= new AlertDialog.Builder(mContext);
	build.setTitle("提示");
	build
.setMessage("您还未绑定支付宝?");
build
.setNegativeButton("考虑一下", null);

build
.setPositiveButton("前去绑定", null);
//build没有提供设置
NegativeButton和
PositiveButton的方法;但观察源码,
//发现dialog本身是有设置的方法.所以先得到dialog,再进行设置:
AlertDialog alertDialog = build.create();
	alertDialog.show();
	//左边的(积极的)设置为黑色
	alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(Color.BLACK);
	//右边的(消极的)设置为黑色
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);

当然:还有其他的方法可以设置:
/**
 * Set a listener to be invoked when the negative button of the dialog is pressed.
 * @param textId The resource id of the text to display in the negative button
 * @param listener The {@link DialogInterface.OnClickListener} to use.
 *
 * @return This Builder object to allow for chaining of calls to set methods
 */

bulid.setNegativeButton(int textId,final OnClickListener listener);

另外: 
build.create();得到AlertDialog
源码:
/**
 * Creates an {@link AlertDialog} with the arguments supplied to this
 * builder and immediately displays the dialog.
 * <p>
 * Calling this method is functionally identical to:
 * <pre>
 *     AlertDialog dialog = builder.create();
 *     dialog.show();
 * </pre>
 */
public AlertDialog show() {
    final AlertDialog dialog = create();
    dialog.show();
    return dialog;
}

	AlertDialog 还可以通过继承来重写某些方法,实现new对象
	AlertDialog dialog=new AlertDialog.Builder(mContext);。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下方式来设置AlertDialog标题和内容的字体大小以及按钮的字体颜色: 1. 设置标题和内容的字体大小 ```kotlin val dialog = AlertDialog.Builder(context).create() dialog.setTitle("Title") dialog.setMessage("Message") // 设置标题和内容的字体大小 val titleTextView = dialog.findViewById<TextView>(android.R.id.alertTitle) titleTextView?.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f) val messageTextView = dialog.findViewById<TextView>(android.R.id.message) messageTextView?.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f) dialog.show() ``` 在上面的代码,我们获取了AlertDialog的标题和内容的TextView,并使用setTextSize()方法来设置它们的字体大小。 2. 设置按钮的字体颜色 ```kotlin val dialog = AlertDialog.Builder(context).create() dialog.setTitle("Title") dialog.setMessage("Message") // 设置按钮的字体颜色 dialog.setOnShowListener { val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE) positiveButton.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary)) val negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE) negativeButton.setTextColor(ContextCompat.getColor(context, R.color.colorAccent)) } dialog.show() ``` 在上面的代码,我们在AlertDialog显示之后,获取了PositiveButton和NegativeButton的Button对象,并使用setTextColor()方法来设置它们的字体颜色。我们使用ContextCompat.getColor()方法来获取颜色资源的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值