android - 从警报对话框中删除黑色背景 - Android

标签 android android-layout dialog android-alertdialog customdialog

我正在开发一个 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);

以上代码运行良好,但对话框显示为黑色。

这是屏幕截图:

Screen shot with black surroundings

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

对话框代码如下:

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

}

国家列表.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 上访问了许多类似的链接,但没有一个有用。

最佳答案

Hope this will help you a lot

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

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

关于android - 从警报对话框中删除黑色背景 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434085/

相关文章:

android - 在 React Native 中设置原生 UISplitViewController

c# - 无法在 Android 上获取 SignalA 以连接到 SignalR 后端

android - 返回应用时的静态变量null

java - 在将搜索字符串指定到我的 EditText 后,如何开始在回收器 View 中搜索匹配项

android - 官方 Android 滑动/选项卡示例包括已弃用的功能 : Is there an alternative sample?

jQuery 对话框上的 Javascript 函数关闭

Android 通用应用方法

java - 在 Android 中动态创建多个 ListView

jQuery UI 对话框边框在 1.12.1 中不再工作

Javascript将单选按钮添加到DOM中的对话框