【问题标题】:AlertDialog changing look of font (size, colour, align)AlertDialog 更改字体外观(大小、颜色、对齐)
【发布时间】:2023-03-22 09:05:01
【问题描述】:

我正在尝试在我的游戏中更改我的 AlertDialog 的属性,目前我正在用这种方式构建它:

private static final int DIALOG_INFO = 1;

private Dialog dialog;  

@Override  
protected Dialog onCreateDialog(int id)  
{  
    AlertDialog.Builder builder = new AlertDialog.Builder(this);  

    switch (id)   
    {       
        case DIALOG_INFO:  

            builder.setTitle("Game Information - Diego 1.0.1");  
            builder.setIcon(R.drawable.icon);  
            builder.setMessage("Test there \n\n");  
            builder.setCancelable(true);  

            dialog = builder.create();  
            dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);                
            break;  
    }   
    return dialog;  
}

如果玩家点击了某个按钮,我会以这种方式显示这个对话框:

showDialog(DIALOG_INFO); 

一切正常,但我决定改变这个对话框中使用的字体的外观,用这种方式:

TextView textView = ((TextView) dialog.findViewById(android.R.id.message));
textView.setTextColor(Color.LTGRAY);
textView.setTextSize(13);
textView.setGravity(Gravity.CENTER);

但代码只有在我把这段代码放在 showDialog(DIALOG_INFO); 之后才有效这意味着我每次显示对话框时都必须创建新对象(TextView textView = ...),我的问题是,我不能在创建对话框时只做一次吗?

提前致谢。

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以为 AlertDialog 设置自己的 contentView:

    .....
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = (ViewGroup) inflater.inflate(R.layout.YOUR_DIALOG_LAYOUT, null);
    builder.setView(rootView);
    .....
    

    并在 xml 中标记您的对话框布局。

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多