如何在android中的警报对话框中设置背景

时间:2015-06-26 07:58:33

标签: android alertdialog

我是android的新手,正在开发一个应用程序。在此应用程序中,我需要打开dialog box关于公司信息。

该框在每个移动设备上都取得了成功,但对话框显示white in some mobileblank in some mobile.

的背景

那么,我怎样才能set background of particular color so display same in all mobiles.

我的对话框警报代码:

private void aboutas() {
        ImageView image = new ImageView(this);

        TextView tv = new TextView(this);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setText(Html
                .fromHtml("<font color='white'><br/> &nbsp &nbsp Version 1.0 <br/><br/> &nbsp &nbsp Developed by </font> <font color='#5C5CFF'> <a href='http://example.com/'>XYZ</a> </font><br/><br/>&nbsp &nbsp <font color='white'> Powered by </font> <font color='#5C5CFF'> <a href='http://example.com/'>XYZ</a> <br/><br/><br/></font> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp <font color='white'> Copyright © 2015 <br/></font>"));

        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        String titlepage = "About wallperper app";
        alertDialog.setTitle(titlepage);
        alertDialog.setView(tv);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // TODO Add your code for the button here.

            }
        });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher_icon);
        alertDialog.show();

    }

我试图解决但不能解决问题。

任何人都有想法?

由于

3 个答案:

答案 0 :(得分:0)

您可以像这样以编程方式设置自定义视图..

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup) getCurrentFocus());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();

然后获得组件的引用

e.g。 Button btn = (Button) dialoglayout.findViewById(R.id.button_id);

答案 1 :(得分:0)

如果您只想要一个轻量级主题并且不了解特定颜色,那么您可以将主题ID传递给AlertDialog.Builder构造函数。

AlertDialog.Builder(this, AlertDialog.THEME_HOLO_LIGHT)...

AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)...

或者您可以创建自定义alertDialog并使用xml布局。在布局中,您可以设置背景颜色和文本颜色。

这样的事情:

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater)ActivityName.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout,(ViewGroup)findViewById(R.id.layout_root));
dialog.setContentView(view);

答案 2 :(得分:0)

使用所需的背景颜色创建layout xml

TextView,ImageView ..组件放置在布局中。

通知它以获得View

最后将AlertDialog的视图设置为虚增的View