从Android的警告对话框中去除黑色背景

4

我正在开发一款Android应用程序,这里我创建了一个自定义对话框,该对话框出现在设备的完整高度上,但我希望从顶部和底部留出边距,因此我使用以下代码设置了边距:

  WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(alertDialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = Utility.convertDPtoPixel(400, screen);

    alertDialog.show();
    alertDialog.getWindow().setAttributes(lp);

以上代码运行良好,但对话框出现黑色周围。

以下是屏幕截图:

带黑色周围的屏幕截图

现在我想从对话框中删除这些黑色周围。

以下是对话框的代码:

public void showDialog() {
    AlertDialog.Builder builder;
    final AlertDialog alertDialog;
    //final Dialog alertDialog;
    Context mContext;
    mContext = screen;
    LayoutInflater inflater = (LayoutInflater) LoginActivity.screen.getSystemService(LoginActivity.screen.LAYOUT_INFLATER_SERVICE);
    layout = inflater.inflate(R.layout.countrylist, null);
    LinearLayout rellayout = (LinearLayout) layout.findViewById(R.id.rellayout);

    RelativeLayout closeAcessCodeDialog = (RelativeLayout)layout.findViewById(R.id.closeAcessCodeDialog) ;

    ListView listview = (ListView) layout.findViewById(R.id.listview);
    listview.setAdapter(new CustomAdapter1(LoginActivity.screen, countryList));

    builder = new AlertDialog.Builder(LoginActivity.screen);
    builder.setView(layout);
    alertDialog = builder.create();
    alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(alertDialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = Utility.convertDPtoPixel(400, screen);

    alertDialog.show();
    alertDialog.getWindow().setAttributes(lp);


    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                                long id) {
            countryName = idCountry.get(pos);
            System.out.println("Country code is: " + countryName);
            alertDialog.cancel();
            text.setText(countryList.get(pos));
        }
    });

    closeAcessCodeDialog.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            alertDialog.dismiss();
        }
    });

}

countrylist.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="#ffe264"
    app:cardCornerRadius="10dp"
    >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rellayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_above="@+id/listview"
            android:gravity="center_vertical"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Select your Country"
                android:textColor="#000"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_centerInParent="true"
                />

            <RelativeLayout
                android:id="@+id/closeAcessCodeDialog"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_alignParentRight="true"
                android:gravity="center">

                <ImageButton
                    android:id="@+id/closeDialog"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/close_country_dialog" />

            </RelativeLayout>
        </RelativeLayout>


        <ListView
            android:id="@+id/listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="@android:color/transparent"
            android:divider="#e5e5e5"
            android:background="@drawable/list_bacground"
            android:dividerHeight="1px" />

    </LinearLayout>

我访问了许多类似的链接在stackoverflow上,但是没有一个是有用的。


在AndroidManifest中使用Theme.Translucent.NoTitleBar更改活动主题,就完成了。 - Andy Developer
1
如果主题不起作用,请将setBackgroundDrawableResource更改为 alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 希望这可以帮助您。 - Andy Developer
@AndyDeveloper 我尝试使用主题,但它没有起作用。 - Shishupal Shakya
尝试第二个答案。 - Andy Developer
@AndyDeveloper 我已经在代码中使用了第二个选项。 - Shishupal Shakya
显示剩余4条评论
2个回答

7

希望这能对您有所帮助

我认为它的默认主题颜色是黑色,因此您需要检查您的AlertDialog的导入,应该是import android.support.v7.app.AlertDialog;还有一件事,您应该使用自定义背景颜色,如下所示:

alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

1
通过更改包并在show()方法之后放置alertDialog.getWindow().setBackgroundDrawable(),太棒了! - Shishupal Shakya

1
尝试一下。
Dialog alertDialog = new Dialog(this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.yourlayout);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接