java Android:更改自定义 AlertDialog 背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10347545/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:39:48  来源:igfitidea点击:

Android: Change custom AlertDialog background

javaandroidandroid-alertdialogbackground-color

提问by Paulius Vindzigelskis

dialog layout xml:

对话框布局xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/root_view"
     android:padding="3dp"
     android:background="@android:color/white"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >

     //...content...

 </TableLayout>

dialog implementation in map overlay when tapped on pushpin:

点击图钉时地图叠加中的对话框实现:

AlertDialog.Builder builder;

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.map_kap_dialog,
                    (ViewGroup) mapView.findViewById(R.id.root_view));

//prepare info to show...
//info prepared

//other preparations stuff
builder = new AlertDialog.Builder(context);
builder.setView(layout);
dialog = builder.create();
dialog.setInverseBackgroundForced(true);
dialog.setCanceledOnTouchOutside(true);
//show it
dialog.show();

and what I see when test it:

以及我在测试时看到的内容:

enter image description here

enter image description here

So I want that light grey background around dialog box (around square white space) change to white, so it won't look that ugly. Can anyone help me?

所以我希望对话框周围的浅灰色背景(方形空白周围)变为白色,这样它看起来就不会那么难看。谁能帮我?

回答by Caner

I had the same problem, I managed to fix it using a custom dialog like this:

我遇到了同样的问题,我设法使用这样的自定义对话框来修复它:

public class CustomDialog extends Dialog {
    public CustomDialog(Context context, View view) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view);
        getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
    }
}

回答by Hardi Shah

Hey setting your view to builder creates this gray lines on top-bottom,instead you can setView to your dialog.like dialog.setView(layout,0,0,0,0);It will bound your layout to whole dialog.

嘿,将您的视图设置为 builder 会在顶部底部创建此灰线,而不是您可以将视图设置为您的 dialog.like dialog.setView(layout,0,0,0,0);它会将您的布局绑定到整个对话框。

回答by Ε Г И ? И О

Another option is to remove android:background="@android:color/white"from your layout. Then the alert dialog will have its default light grayish background uniformly. (Since you have forced an inverse on a dark theme) At least it would look nice and worth the hassle. Just my two cents.

另一种选择是android:background="@android:color/white"从您的布局中删除。然后警报对话框将统一具有默认的浅灰色背景。(因为你在黑暗主题上强行反转)至少它看起来不错,值得麻烦。只有我的两分钱。