Android: Change custom AlertDialog background

2020-03-06 18:26发布

dialog layout 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

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?

3条回答
We Are One
2楼-- · 2020-03-06 18:32

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);
    }
}
查看更多
够拽才男人
3楼-- · 2020-03-06 18:40

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.

查看更多
不美不萌又怎样
4楼-- · 2020-03-06 18:49

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.

查看更多
登录 后发表回答