简体   繁体   English

Android-自定义AlertDialog背景颜色

[英]Android - Custom AlertDialog Background Color

So I see we can have alertdialogs with gray and white (when setinverse...) background colors. 因此,我看到我们可以使用具有灰色和白色(当setinverse ...时)背景色的Alertdialog。

To learn why I checked themes.xml of the sdk, checking it I was led to drawables and there I realized the alertdialog background is not done programatically but via some images. 为了了解为什么我检查了sdk的themes.xml,检查了它是否导致了可绘制对象,并且在那里我意识到Alertdialog的背景不是以编程方式完成的,而是通过一些图像完成的。 And these images guarantee that there are two gray(or white when inverse color) horizontal lines on top(title area) and bottom(just above button area) of the dialog when we use LayoutInflater to just set a different backgroundcolor. 这些图像保证了当我们使用LayoutInflater设置不同的背景色时,对话框的顶部(标题区域)和底部(按钮区域上方)有两条灰色(反色时为白色)水平线。

So my question is, as LayoutInflator is useless and guessing I have to subclass alertdialog, what do you suggest I do to generate an AlertDialog with a different backgroundcolor? 所以我的问题是,由于LayoutInflator没用,并且猜测我必须将alertdialog子类化,您建议我如何生成具有不同背景色的AlertDialog? What should I override? 我应该覆盖什么?

Instead of using AlertDialog, I ended up using a Dialog. 我没有使用AlertDialog,而是使用了Dialog。 To get a custom look: 获得自定义外观:

1-Create the Dialog and remove the title area(Otherwise you'll get a blank gray area on top): 1-创建对话框并删除标题区域(否则您将在顶部看到空白的灰色区域):

myDialog = new Dialog(this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

2-Design a layout in xml, and set as dialog's content: 2-在xml中设计一个布局,并将其设置为对话框的内容:

myDialog.setContentView(R.layout.mydialog_layout);

3-If the layout is not a rounded rect, it will intersect with the rounded corners of the dialog box. 3-如果布局不是圆角矩形,则它将与对话框的圆角相交。 So, design the layout as a rounded rect: 因此,将布局设计为圆角矩形:

in mydialog_layout.xml: 在mydialog_layout.xml中:

android:background = "@layout/mydialog_shape"

mydialog_shape.xml: mydialog_shape.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle" 
     > 
     <gradient android:startColor="#FF0E2E57" 
     android:endColor="#FF0E2E57" 
            android:angle="225" android:paddingLeft="20dip"/> 

    <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" 
     android:topLeftRadius="5dp" android:topRightRadius="5dp" android:paddingLeft="20dip"/> 
</shape>

4-Add listeners to the buttons in your activity: 4-将侦听器添加到活动中的按钮:

Button button = (Button)myDialog.findViewById(R.id.dialogcancelbutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    myDialog.cancel();
}});

That's about it. 就是这样

I recall reading that not all Android Dialogs are created equally. 我记得曾经读过并非所有Android对话框都是一样创建的。 Therefore, if you don't want to use the dialog that shipped with the device's Android version; 因此,如果您不想使用设备的Android版本随附的对话框,请选择“否”。 You need to code a completely fresh dialog from the ground up. 您需要从头开始编写一个全新的对话框。

Edit: 编辑:

I think you need to override the onCreateDialog with a custom dialog builder class. 我认为您需要使用自定义对话框构建器类覆盖onCreateDialog。 Like I said, I've never done it. 就像我说的,我从来没有做过。 Remember, to keep with the Android MVC style, you need to define the dialog in XML as well. 请记住,要保持Android MVC风格,还需要以XML定义对话框。 If I was going to do it; 如果我要去做的话; I would probably start with the XML layout, then code a custom dialog class using the same methods as a regular dialog builder class. 我可能会从XML布局开始,然后使用与常规对话框生成器类相同的方法编写自定义对话框类。 Sorry to be so vague, I'm still learning Java and Android myself. 抱歉,我仍然在学习Java和Android。

so easy.. 太简单..

Dialog d=builder2.create();
...
d.show();
d.getWindow().setBackgroundDrawableResource(R.drawable.mydialog_shape);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM