首页 > 其他分享 >android开发:弹框(AlertDialog)和提示信息Toast字体大小颜色设置

android开发:弹框(AlertDialog)和提示信息Toast字体大小颜色设置

时间:2022-11-01 13:32:57浏览次数:71  
标签:Toast 字体大小 dialog builder AlertDialog 提示信息 mToast TextView

一、AlertDialog:

 

AlertDialog.Builder builder = new AlertDialog.Builder(ReturnActivity.this, android.support.design.R.style.Base_Theme_AppCompat_Dialog_Alert);
builder.setTitle("温馨提示");
builder.setMessage("是否进行下一个病人?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialogInterface, int i) {
    //处理你的操作
    
  }
});
builder.setNegativeButton("否", null);
AlertDialog dialog = builder.create();
//弹框设置背景颜色
final Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
dialog.show();
//弹框设置字体颜色
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
try {
  //获取mAlert对象
  Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
  mAlert.setAccessible(true);
  Object mAlertController = mAlert.get(dialog);
  //获取mMessageView并设置大小颜色
  Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
  mMessage.setAccessible(true);
  TextView mMessageView = (TextView) mMessage.get(mAlertController);
  mMessageView.setTextColor(Color.BLUE);
  //mMessageView.setTextSize(30);
  //获取mTitleView并设置大小颜色
  Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
  mTitle.setAccessible(true);
  TextView mTitleView = (TextView) mTitle.get(mAlertController);
  mTitleView.setTextColor(Color.BLUE);
  //mTitleView.setTextSize(30);
} catch (NoSuchFieldException e) {
  e.printStackTrace();
} catch (IllegalAccessException e) {
  e.printStackTrace();
}

 

二、Toast:

 

private Toast mToast;
private void showTip(final String str){
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), "",
Toast.LENGTH_SHORT);
LinearLayout layout = (LinearLayout) mToast.getView();
TextView tv = (TextView) layout.getChildAt(0);
tv.setTextSize(18);
          tv.setTextColor(R.color.white);
}
//mToast.cancel();
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.setText(str);
mToast.show();
}
});
}

例如:Toast提示"登录成功",直接使用showTip("登录成功")即可。

注意:fragment里面使用时将runOnUiThread替换成getActivity().runOnUiThread即可。

 



标签:Toast,字体大小,dialog,builder,AlertDialog,提示信息,mToast,TextView
From: https://blog.51cto.com/u_11269274/5813399

相关文章

  • Android Toast详解
    一般的Toast我就不说了,我这里主要讲一下,自定义toast包括自定义内容和显示的位置现在Toast有了新情况,在安卓11,现在大家都用SnackBar   效果图 Toast代码priva......
  • vue3+vant 引入Dialog Toast都会失败报错not defined
    今天在封装vant组件的时候,刚好要用到toast提示信息的组件,索性就按照官网提供的引入方法进行正常的引入,嘿,好家伙,一顿操作下来后发现竟然报Toast未定义,这就纳闷了,明明步骤都......
  • 调整el-table的行高(单元格高度)及单元格字体大小
    <el-table:row-style="{height:'20px'}":cell-style="{padding:'0px'}"style="font-size:10px"></el-table>说明:行高到一定程度之后便不能缩小箴言:因为这些东......
  • win10新版无法设置图标字体大小的解决办法
    1803版后,系统高级显示设置中只有 显示信息,而没有具体的文本字体大小设置了,所以目前只能通过注册表来实现。WindowMetrics子键 HKEY_CURRENT_USER\ControlPanel\Deskto......
  • [FAQ] MySQL Workbench 设置界面字体大小
    MySQLWorkbench连接数据库后,点击右上角的图标,进入Fonts&Colors设置。示例: Ref:MySQLWorkbenchLink:https://www.cnblogs.com/farwish/p/16805924.html......
  • android开发:弹框(AlertDialog)和提示信息Toast字体大小颜色设置
    一、AlertDialog: AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());builder.setTitle("温馨提示");builder.setMessage("是否进行下一个病人......
  • vscode怎么使用滚轮调整字体大小
    在使用webstorm的时候,可以使用滚轮调整字体大小,习惯之后,在vscode中总是在设置中调整字体大小,感觉很不方便。后面发现可以通过设置通过鼠标滚轮滑动来调整字体大小。步骤如......
  • qml text显示不同字体大小
    Text{id:label_valy:99width:640height:124horizontalAlignment:Text.AlignHCentercolor:"#0086D1"......
  • 封装一个echart字体大小自适应函数
    //自适应echart字体大小exportconstfontSize=(res)=>{letclientWidth=window.innerWidth||document.documentElement.clientWidth||document.body.clien......
  • navicat 如何调整查询区域字体大小
    Navicat是一套快速、可靠和全面的数据库管理工具,专门用于简化数据库管理和降低管理成本。Navicat图形界面直观,提供简便的管理方法,设计和操作MySQL、MariaDB、SQLServer、O......