android - 删除 AlertDialog 背景

标签 android 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

任何帮助将不胜感激。

最佳答案

尝试

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

关于android - 删除 AlertDialog 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36332029/

相关文章:

Android:LinearLayout 占据 100% 的 View

android - 结合服务处理暂停或停止的 Activity

android - 无法在我的 Android 程序中启动服务 Intent,这是怎么回事?

安卓 RxJava AlertDialog

java - 无法添加窗口 - token null 不适用于广播接收器 OnReceive 内的应用程序

android - Alertdialog 显示图像和标题

java - 使用多个标记创建折线

java - Gson/Volley Android 从响应中获取消息而不是数据

android - 如何防止使用自定义主题的 AlertDialog 全屏显示?

android - 在 PreferenceActivity 中获取根 ViewGroup