对话框背景

时间:2017-10-22 16:26:53

标签: java android

好的,我正在制作手机应用程序。 我为我的对话框创建了一个函数,并在需要时调用该函数。

我希望能够更改对话窗口,即:不同的颜色和线条以及将文本分开的东西。

我已经浏览了整个互联网,我无法找到任何可以帮助我的东西。

这是我正在调用的函数....

public void userNameRequired(View view)
{
    AlertDialog.Builder builder = new AlerdDialog.Builder(this);
    TextView newMessage = new TextView(this);
    newMessage.setText(" User Name is Required to be between 7 and 20    caracters long.");
    newMesage.setGravity(Gravity.CENTER_HORIZONTAL);
    builder.setView(newMessage);

    TextView title = new TextView(this);
    title.setText("!! NOTICE !!");
    title.setGravity(Gravity.CENTER);
    title.setTextSize(28);
    title.setTextColor(Color.RED);

    builder.setCustomTitle(title);

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {

        }
    });

    builder.show();
}

2 个答案:

答案 0 :(得分:0)

pha 所述,您可以为警报对话框指定自定义主题:

How to change theme for AlertDialog

答案 1 :(得分:0)

解决方案1:

  1. 打开colors.xml
  2. 定义这样的颜色:<color name="background_dark">#3F51B5</color>
  3. 在对话框中调用show()后,设置背景颜色如下:

    builder.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
    
  4. 解决方案2:

    1. 定义自定义主题

      //assuming you have `@color/White` and `@color/colorPrimaryDark` defined in `colors.xml`
      <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
          <item name="android:textColor">@color/White</item>
          <item name="android:textStyle">bold</item>
          <item name="android:headerDividersEnabled">true</item>
          <item name="android:typeface">normal</item>
          <item name="android:background">@color/colorPrimaryDark</item>
      </style>
      
    2. 设置样式

      AlertDialog.Builder builder = new AlerdDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
      
    3. 所以,这就是你的代码应该是这样的:

          public void userNameRequired(View view)
          {
             AlertDialog.Builder builder = new AlerdDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
             TextView newMessage = new TextView(this);
             newMessage.setText(" User Name is Required to be between 7 and 20    caracters long.");
             newMesage.setGravity(Gravity.CENTER_HORIZONTAL);
             builder.setView(newMessage);
      
             TextView title = new TextView(this);
             title.setText("!! NOTICE !!");
             title.setGravity(Gravity.CENTER);
             title.setTextSize(28);
             title.setTextColor(Color.RED);
      
             builder.setCustomTitle(title);
      
             builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
             {
                 public void onClick(DialogInterface dialog, int id)
                 {
      
                 }
             });
      
             builder.show();
          }
      
      AlertDialog alertDialog = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme)
              ...
              .create();