删除AlertDialog背景

时间:2016-03-31 11:24:47

标签: android alertdialog

我的AlertDialog中有一个自定义布局,里面有ListView。现在我要删除AlertDialog的黑色背景。

           AlertDialog.Builder builder = new AlertDialog.Builder(this);
            LayoutInflater inflater = this.getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.rate_card_layout, null);
            Button close = (Button) dialogView.findViewById(R.id.btnClose);
            close.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    dialog.dismiss();
                }
            });


            ListView lv = (ListView)dialogView.findViewById(R.id.listView1);
            RateListAdapter rLAdapter = new RateListAdapter(SActivity.this,
                    listItemsArray);
            lv.setAdapter(rLAdapter);

            builder.setView(dialogView);
            final Dialog dialog = builder.create();

            dialog.show();

我尝试添加

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
像往常一样。但这不起作用。

enter image description here

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

尝试

private void updateLayoutParams() {
    Window window = getDialog().getWindow();
    WindowManager.LayoutParams params = window.getAttributes();
    params.dimAmount = 0.2f;

    int margin = getResources().getInteger(R.integer.common_margin);
    DisplayMetrics metrics = new DisplayMetrics();
    ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
    params.width = metrics.widthPixels-margin;

    window.setAttributes(params);
    window.setBackgroundDrawableResource(android.R.color.transparent);
    getDialog().setCanceledOnTouchOutside(false);

}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.d(TAG, "onConfigurationChanged");

    updateLayoutParams();
}

答案 1 :(得分:0)

尝试使用此选项来自定义主题。也声明AlertDialogCustom iin样式 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));

然后按照你想要的样式:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">10sp</item>
         ............. //background etc
    </style>
</resources>

答案 2 :(得分:0)

试试这个

在style.xml中添加自定义对话框样式

 <style name="DialogTheme" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">@android:color/transparent</item>        
    </style>

活动

Dialog dialog = new Dialog(this, R.style.DialogTheme);  

OR

AlertDialog.Builder builder=new AlertDialog.Builder(this,R.style.DialogTheme);