Android AlertDIalog删除不必要的空间

Android AlertDIalog删除不必要的空间,android,android-alertdialog,Android,Android Alertdialog,我使用自定义布局在自定义位置显示AlertDialog 当我显示我的警报对话框时,我在视图的右侧得到了更多的空白,即使我的自定义布局没有那个空白 我没有得到太多的帮助 请查看所附图片了解详细信息 这是我的自定义对话框.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android=

我使用自定义布局在自定义位置显示AlertDialog

当我显示我的警报对话框时,我在视图的右侧得到了更多的空白,即使我的自定义布局没有那个空白

我没有得到太多的帮助

请查看所附图片了解详细信息

这是我的自定义对话框.xml

  <?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatToggleButton
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="15dp"
            android:layout_weight="1"
            android:background="@drawable/toggle"
            android:checked="true"
            android:textOff="Camera"
            android:textOn="Camera" />

        <androidx.appcompat.widget.AppCompatToggleButton
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="15dp"
            android:layout_weight="1"
            android:background="@drawable/toggle"
            android:checked="true"
            android:textOff="Lidar"
            android:textOn="Lidar" />

        <androidx.appcompat.widget.AppCompatToggleButton
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="15dp"
            android:layout_weight="1"
            android:background="@drawable/toggle"
            android:checked="true"
            android:textOff="Sonar"
            android:textOn="Sonar" />

    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
这是我正在使用的样式

    <style name="CustomDialog" parent="Theme.AppCompat.Light.Dialog.Alert">>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
<!--        <item name="android:windowBackground">@null</item>-->
        <item name="android:windowNoTitle">true</item>
    </style>
>
包装内容
包装内容
真的
如果您需要更多信息,请随时询问


谢谢

我尝试过更改窗口属性大小,但没有达到您的要求。然后我用
@style/Theme.AppCompat.Light.Dialog.Alert
替换了您现有的样式
Theme.AppCompat.Dialog
。现在让我们来控制布局

改为:

    <style name="CustomDialog" parent="@style/Theme.AppCompat.Dialog">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:windowNoTitle">true</item>
    </style>
在您的java代码中,我不确定是否进行了更改,所以我将粘贴它

private void showDialogHere() {
        
        AlertDialog.Builder obstacleDialogBuilder = new AlertDialog.Builder(this, R.style.CustomDialog);
        obstacleDialogBuilder.setMessage(null);

        LayoutInflater inflaterObstacle = this.getLayoutInflater();
        View obstacleDialogView = inflaterObstacle.inflate(R.layout.my_dialog, null);
        obstacleDialogBuilder.setView(obstacleDialogView);

        AlertDialog obstacleAlertDialog = obstacleDialogBuilder.create();
        obstacleAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = obstacleAlertDialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        params.gravity = Gravity.TOP | Gravity.RIGHT;
        params.x = 100;   //x position
        params.y = 100;   //y position

        obstacleAlertDialog.show();
    }
让我知道这是否有效

我将进一步尝试理解为什么该样式不允许更改对话框大小。如果我发现任何有用的东西,我会发布


我发现这个解决方案也适用于您的情况。

尝试线性布局线性布局在我的自定义对话框中。@ArthTilva您想让我删除约束布局吗?@Arpit Patel,您找到解决方案了吗?@Rajasekhar不,我没有找到解决方案谢谢您的回答,我会检查并让您知道
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:padding="16dp">

    <androidx.appcompat.widget.AppCompatToggleButton
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:background="#43212121"
        android:checked="true"
        android:textOff="Camera"
        android:textOn="Camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/two"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatToggleButton
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#43212121"
        android:checked="true"
        android:textOff="Lidar"
        android:textOn="Lidar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatToggleButton
        android:id="@+id/three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:background="#43212121"
        android:checked="true"
        android:textOff="Sonar"
        android:textOn="Sonar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/two"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
private void showDialogHere() {
        
        AlertDialog.Builder obstacleDialogBuilder = new AlertDialog.Builder(this, R.style.CustomDialog);
        obstacleDialogBuilder.setMessage(null);

        LayoutInflater inflaterObstacle = this.getLayoutInflater();
        View obstacleDialogView = inflaterObstacle.inflate(R.layout.my_dialog, null);
        obstacleDialogBuilder.setView(obstacleDialogView);

        AlertDialog obstacleAlertDialog = obstacleDialogBuilder.create();
        obstacleAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = obstacleAlertDialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        params.gravity = Gravity.TOP | Gravity.RIGHT;
        params.x = 100;   //x position
        params.y = 100;   //y position

        obstacleAlertDialog.show();
    }