【问题标题】:androidx.appcompat.app.AlertDialog always changing from activity layout to white backgroundandroidx.appcompat.app.AlertDialog 总是从活动布局变为白色背景
【发布时间】:2020-02-24 20:02:23
【问题描述】:

我的 alertdialog 总是将活动布局更改为白页。使用相机时出现对话框。尝试将背景更改为透明但没有运气。有任何想法吗?附上模拟器的截图。希望在使用相机时出现警报对话框,而不是让白色背景覆盖整个屏幕。

代码:

  AlertDialog alertDialog = new AlertDialog.Builder(this)
                //set icon
                .setIcon(android.R.drawable.ic_dialog_alert)
                //set title
                .setTitle("Kan ikke finne produktet i databasen")
                //set message
                .setMessage("Vil du søke manuelt?")
                // Background
                //set positive button
                .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(ScanActivity.this, ManualActivity.class);
                        startActivity(intent);
                    }
                })
                //set negative button
                .setNegativeButton("Nei", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent(ScanActivity.this, ScanActivity.class);
                        startActivity(intent);                        }
                })

                .show();

XML

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.journeyapps.barcodescanner.BarcodeView
        android:id="@+id/zxing_barcode_surface"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        app:zxing_framing_rect_width="225dp"
        app:zxing_framing_rect_height="275dp"
        />

    <com.journeyapps.barcodescanner.ViewfinderView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/zxing_viewfinder_view"
        app:zxing_possible_result_points="#FFC300"
        app:zxing_viewfinder_laser="#EA1818"
        app:zxing_result_view="#2DA818"
        app:zxing_viewfinder_mask="@color/zxing_custom_viewfinder_mask"
        />

    <TextView
        android:id="@+id/zxing_status_view"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/zxing_transparent"
        android:text="@string/barcode_help"
        android:textColor="@color/zxing_status_text"
        />

</merge>

Screenshot from emulator

【问题讨论】:

  • 你能分享你的活动 xml 文件吗?
  • 添加了 xml 布局。

标签: android android-camera android-alertdialog zxing


【解决方案1】:

您忘记在警报对话框中添加setView 指令。

.setView(R.layout.dialog_layout)

假设您的 xml 文件名是 dialog_layout,否则将其更改为正确的。

【讨论】:

  • 这会使对话框消失,并且应用程序在第二次尝试触发触发对话框时崩溃。该活动使用 zxing 扫描仪获得了 2 个视图。
【解决方案2】:

试试这个可能对你有帮助

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

【讨论】:

  • 这是我之前尝试过的,但它只是给了我一个“无法解决方法”的错误
  • 这行得通:this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 但是背景现在是黑色的,就好像相机没有在后台运行一样。
  • 尝试将背景颜色从 Color.TRANSPARENT 更改为任何其他
  • 背景也随着任何其他颜色而变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多