Android的 - 自定义AlertDialog背景颜色自定义、颜色、背景、Android

2023-09-13 23:49:05 作者:醉饮千觞不知愁

因此​​,我认为我们可以有alertdialogs有灰色和白色(当setinverse ...)的背景颜色。

So I see we can have alertdialogs with gray and white (when setinverse...) background colors.

要了解为什么我检查的SDK的themes.xml,检查它,我被带到可绘制在那里我认识了alertdialog背景没有做编程,而是通过一些图片。而这些图像保证有两个灰色(或白色时,反色),当我们使用LayoutInflater只设置了不同的backgroundColor在该对话框的顶部(标题区域)和底部(正上方的按钮区域)的水平线。

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.

所以我的问题是,作为LayoutInflator是无用的,猜测我有子类alertdialog,你有什么建议我做,以生成一个AlertDialog用不同的backgroundColor?我应该重写?

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?

推荐答案

而不是使用AlertDialog的,最后我用一个对话框。为了得到一个自定义外观:

Instead of using AlertDialog, I ended up using a Dialog. To get a custom look:

1,创建对话框,并删除标题区(否则,你就顶得上一个空白灰色区域):

1-Create the Dialog and remove the title area(Otherwise you'll get a blank gray area on top):

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

2,设计XML的布局,并设置对话框的内容:

2-Design a layout in xml, and set as dialog's content:

myDialog.setContentView(R.layout.mydialog_layout);

3-如果布局不是圆角的矩形,将与对话框的圆角相交。因此,设计布局为圆角的矩形:

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:

在mydialog_layout.xml:

in 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,添加监听器的按钮在您的活动:

4-Add listeners to the buttons in your activity:

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();
}});

这就是它。