透明AlertDialog Android

时间:2014-05-15 05:53:46

标签: android alertdialog android-alertdialog

我想让我的AlertDialog透明,以下是我的代码和它应该透明的行是不起作用。

AlertDialog.Builder builder;
            AlertDialog alertDialog;

            context = getActivity();

            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.moreinfotable,null);

            ListView list = (ListView) layout.findViewById(R.id.dialog_listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    ViewImage.this.getActivity(), R.layout.interior_listview,
                    R.id.interior_list_text, values);
            list.setAdapter(adapter);

            builder = new AlertDialog.Builder(context);
            builder.setView(layout);

            alertDialog = builder.create();

//THE LINE THAT DOESNT WORK// alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            alertDialog.show();

有人可以帮忙吗?谢谢。

这就是我想要做的。 enter image description here

1 个答案:

答案 0 :(得分:3)

使用这种方式

alertDialog.show();
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.argb(0, 200, 200, 200)));

我在自定义提醒对话框中执行了此操作。

值类似于alpha,red,green,blue。范围从0到255.更改alpha值以获得不同的透明度

我不知道这是否会对你有所帮助。这就是我创建对话框的方式(它提示用户在没有登录的情况下尝试应用程序)

public void promptUser()
{       
    final Dialog requestDialog= new Dialog(MyActivity.this);
    requestDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    requestDialog.setContentView(R.layout.lyt_prompt_user);

    TextView requestTitle = (TextView) requestDialog.findViewById(R.id.request_title);
    TextView requestText = (TextView) requestDialog.findViewById(R.id.request_text);
    Button acceptButton = (Button) requestDialog.findViewById(R.id.request_accept);
    Button rejectButton = (Button) requestDialog.findViewById(R.id.request_reject);

    requestTitle.setText("Don't want to login?");
    requestText.setText("You can still explore the app with limited functionality.");

    acceptButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // do something
            requestDialog.dismiss();
            finish();
            startActivity(intent);
        }
    });

    rejectButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            requestDialog.dismiss();
            (MyActivity.this).finish();
        }
    });

    requestDialog.setCancelable(false);
    requestDialog.show();
    requestDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.argb(0, 200, 200, 200)));
}

我尝试将alpha从0更改为90,它显示出微弱的白色背景,有些玻璃般的感觉。