对话框无法设置宽度窗口的大小

时间:2018-07-10 20:48:10

标签: android dialog

您好,我在设置对话框大小以适合窗口宽度时遇到问题。

我在这里尝试了以下几种答案: Android dialog width Android get full width for custom Dialog

但是我无法达到手机宽度的全部大小。它实际上具有TOP(重力)的属性,但宽度却没有。

我使用的代码是:

    package bem.banorte.bem;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Created by IDS Comercial on 09/07/2018.
 */

public class ThirdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.CustomDialogTheme));

        TextView title = new TextView(this);
        // You Can Customise your Title here
        title.setText("Error en la aplicacion");
        // title.setBackgroundColor(Color.);
        title.setPadding(0, 30, 0, 30);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(20);

        builder.setCustomTitle(title);

        //antes era alertDialog
        Dialog dialog = builder.create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());
        lp.gravity = Gravity.TOP | Gravity.LEFT;
        lp.x = 0;   //x position
        lp.y = 85;   //y position
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

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

    }

}

样式的代码是这样的:

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">@color/black</item>
        <item name="android:windowBackground">@color/warningA</item>
        <item name="android:background">@color/warningA</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:typeface">normal</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

活动如下:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bem.banorte.bem.ThirdActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

enter image description here

1 个答案:

答案 0 :(得分:1)

更新:

在对build.gradle和所有OP进行交叉检查之后,这似乎是设备特定的问题。这段代码在Nexus 5(api 23)真实设备和Pixel2(Api 23)AVD中不会占用全角。

我们怀疑-@android:style/Theme.Dialog在此Motorola G Play(Android 6.0.1 api 23)设备中不相同。

原文:

您的拨号正在获取完整宽度。我尝试了您的代码,但在您的主题中按照以下方式切换了颜色-

<item name="android:windowBackground">@android:color/holo_blue_light</item>
<item name="android:background">@android:color/holo_red_light</item>

我看到了

  • Occupied Dialog width