如何更改自定义对话框的透明度

时间:2013-11-26 19:45:57

标签: android xml android-alertdialog

对话框的

xml:

<TextView
    android:id="@+id/redeemFailedTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:text="SORRY,\nYOU HAVE INSUFFICIENT\n POINTS FOR THIS\n REDEMPTION." 
    android:gravity="center"/>

<ImageView
    android:id="@+id/dialogCrossImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/redeemFailedTextView"
    android:src="@drawable/dialog_ic_close_focused_holo_light" />

对话框的java代码:

final Dialog dialog=new Dialog(getActivity());
                    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.dialog_redeem_failed);

                    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

                    dialog.show();

                    ImageView dialogCrossImageView=(ImageView) dialog.findViewById(R.id.dialogCrossImageView);
                    dialogCrossImageView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });

呈现对话框:

enter image description here

为什么对话框的背景是黑色的?我怎样才能让它更透明?如何更改维护透明度的背景颜色?

1 个答案:

答案 0 :(得分:3)

您可以尝试以下操作:

ColorDrawable dialogColor = new ColorDrawable( Color.BLACK);
dialogColor.setAlpha(150); //(0-255) 0 means fully transparent, and 255 means fully opaque
dialog.getWindow.setBackgroundDrawable(ColorDrawable)