【问题标题】:Change background colour of android dialog更改android对话框的背景颜色
【发布时间】:2012-10-19 11:26:27
【问题描述】:

我有一个使用 AlertDialog.Builder 构建的对话框,我想更改对话框的背景颜色。

我一直在 Internet 上阅读到可以使用 ContextThemeWrapper(使用 API 10)来完成,但它不起作用。

我正在做的是:

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, R.style.MyDialogTheme);
AlertDialog alertDialog = new AlertDialog.Builder(wrapper)).create();  

<style name="MyDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:background">#FFFFFF</item>
</style>

为什么这不起作用?

提前致谢!

【问题讨论】:

  • 对话框是否实际显示?你永远不会调用 alertDialog.show()

标签: android background dialog


【解决方案1】:

alertDialog xml 文件,在这里:

https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/layout/alert_dialog_micro.xml

只需更改它们的值:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(_title);
    builder.setMessage(_message);
    //....
    AlertDialog dialog = builder.show();

    int objeto = getResources().getIdentifier("buttonPanel","id","android");
    View vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.RED);
    }
    objeto = getResources().getIdentifier("topPanel", "id", "android");
    vistaObjeto = dialog.findViewById(objeto);
    if (vistaObjeto != null){
        vistaObjeto.setBackgroundColor(Color.YELLOW);
    }
    objeto = getResources().getIdentifier("alertTitle","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        ((TextView)vistaObjeto).setTextColor(Color.BLACK);
    }
    objeto = getResources().getIdentifier("titleDivider","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.GREEN);
    }
    objeto = getResources().getIdentifier("contentPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.BLUE);
    }
    objeto = getResources().getIdentifier("buttonPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.MAGENTA);
    }
    objeto = getResources().getIdentifier("parentPanel","id","android");
    vistaObjeto=dialog.findViewById(objeto);
    if (vistaObjeto!=null){
        vistaObjeto.setBackgroundColor(Color.CYAN);
    }
    return dialog;
}

【讨论】:

【解决方案2】:

对自定义对话框使用以下代码:

protected Dialog onCreateDialog(int dialogId) {
    LayoutInflater inflater = 
        (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final View viewMessEdit = inflater.inflate(
        R.layout.example,
        (ViewGroup) findViewById(R.id.dialog_mess_edit_root));
    builder.setView(viewMessEdit);
    viewMessEdit.setBackgroundResource(R.color.pink_dark);
}

然后点击这个链接:

【讨论】:

    【解决方案3】:

    我已经用 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT 修改了我的 AlertDialog,所以我这样写了我的构建器:

    AlertDialog.Builder dialog = new AlertDialog.Builder(ApplicationCertificatesListFragment.this.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2013-12-29
      • 2013-08-23
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多