简体   繁体   中英

Android: Change custom AlertDialog background

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:

在此输入图像描述

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?

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

嘿将视图设置为构建器会在上下创建这条灰色线条,而不是将setView设置为对话框。dialog.setView(layout,0,0,0,0);它会将布局绑定到整个对话框。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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