Java Android - 自定义 AlertDialog 背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3995058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Custom AlertDialog Background Color
提问by ahmet emrah
So I see we can have alertdialogs with gray and white (when setinverse...) background colors.
所以我看到我们可以使用灰色和白色(当 setinverse...)背景颜色的警报对话框。
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. 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.
为了了解为什么我检查了 sdk 的 themes.xml,检查它我被引导到了可绘制对象,在那里我意识到警报对话框背景不是以编程方式完成的,而是通过一些图像完成的。并且这些图像保证当我们使用 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? What should I override?
所以我的问题是,由于 LayoutInflator 没用,我猜我必须继承 alertdialog,你建议我做什么来生成具有不同背景颜色的 AlertDialog?我应该覆盖什么?
采纳答案by ahmet emrah
Instead of using AlertDialog, I ended up using a Dialog. To get a custom look:
我没有使用 AlertDialog,而是使用了 Dialog。要获得自定义外观:
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. So, design the layout as a rounded rect:
3-如果布局不是圆角矩形,它将与对话框的圆角相交。因此,将布局设计为圆角矩形:
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.
就是这样。
回答by Martinez
I recall reading that not all Android Dialogs are created equally. Therefore, if you don't want to use the dialog that shipped with the device's Android version; You need to code a completely fresh dialog from the ground up.
我记得读过并不是所有的 Android 对话框都是平等创建的。因此,如果您不想使用设备 Android 版本附带的对话框;您需要从头开始编写一个全新的对话框。
Edit:
编辑:
I think you need to override the onCreateDialog with a custom dialog builder class. 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. 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. Sorry to be so vague, I'm still learning Java and Android myself.
我认为您需要使用自定义对话框构建器类覆盖 onCreateDialog。就像我说的,我从来没有做过。请记住,为了与 Android MVC 风格保持一致,您还需要在 XML 中定义对话框。如果我打算这样做;我可能会从 XML 布局开始,然后使用与常规对话框构建器类相同的方法编写自定义对话框类。抱歉这么含糊,我自己还在学习 Java 和 Android。
回答by KnIfER
so easy..
太简单..
Dialog d=builder2.create();
...
d.show();
d.getWindow().setBackgroundDrawableResource(R.drawable.mydialog_shape);