AlertDialog更改字体的外观(大小,颜色,对齐) [英] AlertDialog changing look of font (size, colour, align)
问题描述
我想改变我的比赛里面我AlertDialog的属性,我目前这种方式构建的:
私有静态最终诠释DIALOG_INFO = 1;
私人对话的对话;@覆盖
保护对话框onCreateDialog(INT ID)
{
AlertDialog.Builder建设者=新AlertDialog.Builder(本); 开关(ID)
{
案例DIALOG_INFO: builder.setTitle(游戏资讯 - 迭戈1.0.1);
builder.setIcon(R.drawable.icon);
builder.setMessage(测试有\\ n \\ n);
builder.setCancelable(真); 对话框= builder.create();
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
打破;
}
返回对话框;
}
块引用>如果玩家点击某个按钮,我展示这个对话框,是这样的:
的ShowDialog(DIALOG_INFO);
一切正常,但我决定改变此对话框中使用字体的外观,这种方式:
TextView的TextView的=((TextView中)
dialog.findViewById(android.R.id.message));结果
textView.setTextColor(Color.LTGRAY);结果
textView.setTextSize(13);结果,
textView.setGravity(Gravity.CENTER);
块引用>不过,code只能工作,如果我把这个code的ShowDialog(DIALOG_INFO)之后;这意味着我必须创建新的对象(TextView的的TextView = ...)每次我展示对话框,然后我的问题是,我不能做它只有一次,同时创造对话?
先谢谢了。
解决方案您可以设置自己的内容查看的AlertDialog:
.....
LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看rootView =(ViewGroup中)inflater.inflate(R.layout.YOUR_DIALOG_LAYOUT,NULL);
builder.setView(rootView);
.....和标记在XML中的对话框布局。
I'm trying to change properties of my AlertDialog inside of my game, currently I'm building it with this way:
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; }
And if player clicked certain button, I'm showing this dialog with this way:
showDialog(DIALOG_INFO);
Everything works, but I decided to change look of font used in this dialog, with this way:
TextView textView = ((TextView) dialog.findViewById(android.R.id.message));
textView.setTextColor(Color.LTGRAY);
textView.setTextSize(13);
textView.setGravity(Gravity.CENTER);But code works only, if I put this code after showDialog(DIALOG_INFO); which means I have to create new object (TextView textView = ...) everytime I'm showing dialog, and my question is, can't I do it only once while creating dialog?
Thanks in advance.
解决方案You can set your own contentView for AlertDialog:
..... LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rootView = (ViewGroup) inflater.inflate(R.layout.YOUR_DIALOG_LAYOUT, null); builder.setView(rootView); .....
And markup your dialog layout in xml.
这篇关于AlertDialog更改字体的外观(大小,颜色,对齐)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!