首页 > 解决方案 > Android 弹出对话框屏幕大小在 android Oreo 中没有变化

问题描述

我正在做安卓项目。我想在我的活动中显示一个弹出对话框。我已经实现了它(下面附上的所有代码)。但我的问题是,当我在 android 中运行这个项目时,弹出窗口显示如下。我需要增加这个弹出对话框的宽度。我尝试了一些解决方案,但对我不起作用。请帮我解决这个问题。提前致谢。

在此处输入图像描述

这是我的代码

xml文件——

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lv_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="MSGP3006"
        android:textColor="@color/Black"
        android:textStyle="bold"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/type"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="QTY:2 | Weight : 500g | Discount::25%"/>

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_marginBottom="@dimen/dimen_5dp"
        android:layout_gravity="bottom">

        <TextView
            android:id="@+id/amount"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Rs.360.00"
            android:gravity="end"
            android:textColor="@color/Sky_Dark_Blue"
            android:textSize="22sp"/>

    </LinearLayout>

</LinearLayout>

java文件——

public class AddInvoiceDialog extends Dialog {

    public Activity activity;
    public Dialog dialog;
    public Button btn_add;

    public AddInvoiceDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.inv_add_new_invoice_popup);

        btn_add = (Button) findViewById(R.id.btn_add);

        btn_add.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                activity.finish();
            }
        });
    }
}

标签: androidpopupwidth

解决方案


在后面添加以下行setContentView(R.layout.inv_add_new_invoice_popup);

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);

您将获得与设备宽度匹配的弹出窗口。

您还需要添加android:orientation="vertical"您的 parent LinearLayout,它包含多个孩子。


推荐阅读