平板电脑上的AlertDialog中的空格顶部

时间:2018-12-09 13:22:57

标签: android alertdialog

我开发了用于计算矩阵的移动应用程序。今天,我想出了一个有趣的问题。在自定义AlertDialog中,顶部仅在平板电脑上显示空格:

enter image description here

当我在智能手机中运行该应用程序时,我没有遇到此问题:

enter image description here

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="@string/text_save_result"
            android:padding="5dp"
            android:paddingStart="25dp"
            android:textStyle="bold"
            android:textSize="20sp"
            android:textColor="@color/white"
            tools:ignore="RtlSymmetry"/>

    <EditText
            android:id="@+id/edt_name_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_enter_name_saving"
            android:inputType="text"
            android:layout_marginTop="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"/>

    <TextView
            android:id="@+id/tv_error"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:drawableStart="@drawable/ic_error"
            android:layout_marginStart="10dp"
            android:gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:drawablePadding="4dp"
            android:visibility="gone"/>

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

        <Button
                android:id="@+id/btn_save"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:text="@string/text_btn_save"
                android:textColor="@color/colorPrimary"/>

        <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:text="@string/text_btn_cancel"
                android:textColor="@color/colorPrimary"/>
    </LinearLayout>

</LinearLayout>

AlertDialog的Java代码:

    package com.whitedeveloper.matrix.alerts;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.whitedeveloper.matrix.R;
import com.whitedeveloper.matrix.instance.SavingInstance;

public class AlertDialogSave extends Dialog {
    public interface CallBackFromAlertDialogSave {
        void callBack(String name);
    }

    private CallBackFromAlertDialogSave callBack;
    private TextView tvError;

    public AlertDialogSave(@NonNull Context context, CallBackFromAlertDialogSave callBack) {
        super(context);
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog_name_saving);

        init();
    }

    private void init() {
        tvError = findViewById(R.id.tv_error);

        final Button btnSave = findViewById(R.id.btn_save);
        final Button btnCancel = findViewById(R.id.btn_cancel);
        final EditText edtName = findViewById(R.id.edt_name_save);
        edtName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (tvError.getVisibility() == View.VISIBLE)
                    tvError.setVisibility(View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!edtName.getText().toString().trim().equals("")) {
                    if (SavingInstance.isNameExisted(getContext(), edtName.getText().toString())) {
                        if (tvError.getVisibility() == View.GONE) {
                            tvError.setVisibility(View.VISIBLE);
                            tvError.setText(R.string.text_existed_already);
                        }
                        return;
                    }
                    callBack.callBack(edtName.getText().toString());
                    hide();
                    dismiss();
                } else
                if (tvError.getVisibility() == View.GONE) {
                    tvError.setVisibility(View.VISIBLE);
                    tvError.setText(R.string.cannot_be_empty);
                }

            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hide();
                dismiss();
            }
        });
    }
}

我不知道发生了什么事? 我哪里错了?请帮助:))

2 个答案:

答案 0 :(得分:0)

您可以尝试设置无标题的自定义样式,因为我想空格是因为这个,请尝试以下操作:

在您的styles.xml中创建对话框的自定义主题:

<style name="MyCustomTheme" parent="Theme.AppCompat.Light.Dialog.Alert">>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>

然后在您的AlertDialogSave类中,将以下内容添加到您的构造函数中:

public AlertDialogSave(@NonNull Context context, CallBackFromAlertDialogSave callBack) {
        super(context, R.style.MyCustomTheme);
        this.callBack = callBack;
    }

尝试一下,看看结果。

答案 1 :(得分:0)

我解决了这个奇怪的问题。 我发现我使用Dialog而不是AlertDialog。