设置警报对话框的自定义标题和图标

时间:2014-05-09 16:16:40

标签: android alertdialog title typeface

知道怎么做吗?这是我的代码片段,困境是我想为我的AlertDialog.builder的标题,消息和按钮文本定制字体。问题是当对话框出现时,没有图标,应该在标题下面的蓝色默认分界线在对话框上方,我的自定义标题正好在消息上方。布局应该是:

(ICON) (TITLE)
(divider)
(Message)
(button1)(button2)

but instead it looks like this:

(divider)

(Title) (Message) (button1) (button2)

There is no icon and the divider is above the window...

Here is my code:

final AlertDialog.Builder endGameDialog = new AlertDialog.Builder(myContext);
    // Setting Dialog
    endGameDialog.setCancelable(false);
    endGameDialog.setMessage("Do you want to play again?");

....

if (myScore == roundChoice) { AlertDialog alert = endGameDialog.create(); alert.requestWindowFeature(Window.FEATURE_LEFT_ICON); TextView titleV = new TextView(myContext); titleV.setTypeface(font); titleV.setTextSize(60); titleV.setTextColor(Color.WHITE); titleV.setShadowLayer(1, 6, -1, redShadow); titleV.setText("You won!"); alert.setCustomTitle(titleV); alert.show(); alert.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher); alert.getWindow().getAttributes(); dialog = true; Button btn1 = alert.getButton(DialogInterface.BUTTON_POSITIVE); Button btn2 = alert.getButton(DialogInterface.BUTTON_NEGATIVE); TextView messageView = (TextView) alert.findViewById(android.R.id.message); messageView.setTypeface(font); messageView.setTextSize(54); messageView.setTextColor(Color.WHITE); messageView.setShadowLayer(1, 6, -1, redShadow); btn1.setTypeface(font); btn1.setTextSize(44); btn1.setTextColor(Color.WHITE); btn1.setShadowLayer(1, 6, -1, redShadow); btn2.setTypeface(font); btn2.setTextSize(44); btn2.setTextColor(Color.WHITE); btn2.setShadowLayer(1, 6, -1, redShadow); }

1 个答案:

答案 0 :(得分:1)

我最好的建议是制作自定义布局并将其分配给自定义对话框 请参阅此参考资料:developer.android.com/guide/topics/ui/dialogs.html

在不使用自定义布局的情况下构建AlertDialog:

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title)
       .setIcon(R.drawable.dialog_icon); // Here you set your icon

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();

但是我不知道你怎么能为标题分配不同的字体。