全屏显示警报对话框

wmvff8tz  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(250)

我的自定义警报对话有问题。我想使用高度 Package 的内容,以便它可以根据我的内容自动调整大小。我已经尝试了所有可能的方法来调整它的大小,但它仍然在全屏显示
我的java代码如下所示

public void showSpinerDialog() {
        Builder adb = new Builder(this.context);
        View v = this.context.getLayoutInflater().inflate(R.layout.dialog_layout_radio, (ViewGroup) null);
        TextView rippleViewClose = (TextView) v.findViewById(R.id.close);
        TextView title = (TextView) v.findViewById(R.id.spinerTitle);
        rippleViewClose.setText(this.closeTitle);
        title.setText(this.dTitle);
        listView = (ListView) v.findViewById(R.id.list);
        final EditText searchBox = (EditText) v.findViewById(R.id.searchBox);
        myAdapterRadio = new MyAdapterRadio(this.context, this.items);
        listView.setAdapter(myAdapterRadio);
        adb.setView(v);

        this.alertDialog = adb.create();
        this.alertDialog.getWindow().getAttributes().windowAnimations = this.style;
        this.alertDialog.setCancelable(false);

        searchBox.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            public void afterTextChanged(Editable editable) {
                myAdapterRadio.getFilter().filter(searchBox.getText().toString());
            }
        });
        rippleViewClose.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SpinnerDialog.this.alertDialog.dismiss();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView t = (TextView) view.findViewById(R.id.text1);
                String selectedID = "";
                for (int j = 0; j < items.size(); j++) {
                    if (t.getText().toString().equalsIgnoreCase(items.get(j).toString())) {
                        pos = j;
                        selectedID = items.get(j).getId();
                    }
                    if (j == i) {
                        items.get(j).setSelected(true);
                    } else {
                        items.get(j).setSelected(false);
                    }
                }
                onSpinerItemClick.onClick(t.getText().toString(), selectedID, pos);
                alertDialog.dismiss();
            }
        });

        this.alertDialog.show();
        this.alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    }

xml如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/llOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/rlTwo"
        android:layout_margin="15dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/spinerTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/select_or_search_items"
            android:textSize="17sp"
            android:visibility="gone"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|left"
            android:visibility="gone"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/ic_search_black" />

            <EditText
                android:id="@+id/searchBox"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="5dp"
                android:background="#ffffff"
                android:inputType="text" />

        </LinearLayout>

        <View
            android:layout_width="wrap_content"
            android:layout_height="0.1dp"
            android:background="#d1d1d1" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#d1d1d1"
            android:dividerHeight="0.1dp">

        </ListView>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rlTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="15dp">

        <TextView
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/Ok"
            android:clickable="true"
            android:padding="10dp"
            android:layout_alignParentRight="true"
            android:text="@string/close"
            android:textSize="16sp" />

    </RelativeLayout>

</RelativeLayout>

风格如下

<style name="AlertDialogTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/colorPrimary</item>
        <item name="colorPrimary">@color/colorAccent</item>
        <item name="colorPrimaryDark">@color/colorAccent</item>
    </style>

我只有一个行项目显示在列表中,它仍然显示在全屏和其他空间是空的。我不知道我错过了什么。让我知道如果有人在这里可以帮助我的对话高度根据我的内容,而不是全屏。谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题