【问题标题】:Custom alert dialog using DialogFragment is not behaving as expected使用 DialogFragment 的自定义警报对话框未按预期运行
【发布时间】:2020-05-26 10:33:50
【问题描述】:

我正在尝试实现一个自定义警报构建器,它应该显示我想要使用的自定义布局。我创建了一个名为“AlertDialogClass”的类,它扩展了 DialogFragment。我正在尝试从片段中显示此警报生成器类。

使用该 AlertDialogClass 时我没有收到任何错误或任何崩溃,但应该显示的布局仅显示白屏。我尝试了很多东西,但似乎没有任何效果。我不明白为什么会发生这种情况或我做错了什么。

这是我的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardBackgroundColor="@color/backgroundElevation2dp"
    app:cardCornerRadius="14dp"
    app:cardElevation="8dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/padding18dp"
        android:gravity="center_horizontal"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentTop="true"
            android:gravity="center_horizontal"
            >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Connection Not Available !!"
            android:textAppearance="@style/CardTextView"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Please Check Your Internet Connection And Try Again"
            android:textAppearance="@style/CardTextViewSmall"
            android:layout_margin="@dimen/margin3dp"
            />

        </LinearLayout>

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/no_connection"
            android:layout_centerInParent="true"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_background1"
            android:text="Try Again"
            android:textAppearance="@style/CardTextView"
            android:layout_marginLeft="@dimen/margin38dp"
            android:layout_marginRight="@dimen/margin38dp"
            android:textAlignment="center"
            android:layout_alignParentBottom="true"
            android:padding="@dimen/padding5dp"
            android:id="@+id/adTryAgain"
            />
    </RelativeLayout>

</androidx.cardview.widget.CardView>

我的警报对话框类。

public class AlertDialogClass extends DialogFragment implements View.OnClickListener {

    private TextView tryAgainButton ;
    private AlertDialogClickInterface clickInterface ;


    public AlertDialogClass(AlertDialogClickInterface clickInterface) {
        this.clickInterface = clickInterface;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        View layout = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog,null);
        tryAgainButton = (TextView)layout.findViewById(R.id.adTryAgain);
        tryAgainButton.setOnClickListener(this);

        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout);

        return dialog;

    }


    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.adTryAgain){
            clickInterface.OnAlertButtonClicked();
        }

    }

    public interface AlertDialogClickInterface {
        void OnAlertButtonClicked();
    }

}

我要显示警报对话框的片段。

try {

    if (CheckConnection.isConnected())
        getDataFromViewModel();

    else {

        AlertDialogClass dialogClass = new AlertDialogClass( this);
        dialogClass.show(getChildFragmentManager(),dialogClass.getTag());
    }
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

预期的布局应该是这样的:

但实际输出是这样的:

这是我在文本视图中使用的样式

<style name="CardTextView" parent="TextAppearance.AppCompat.Widget.ActionBar.Subtitle" >
    <item name="android:textColor">@color/textColorWhite1</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/textSize18</item>
    <item name="android:fontFamily">sans-serif-smallcaps</item>
</style>

<style name="CardTextViewSmall" parent="TextAppearance.AppCompat.Widget.ActionBar.Subtitle" >
    <item name="android:textColor">@color/textColorWhite2</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/textSize14</item>
    <item name="android:fontFamily">sans-serif-smallcaps</item>
</style>

【问题讨论】:

  • 我已经编辑了问题并添加了我用于文本视图的样式。并确保样式文件是否导致问题,我已从布局中删除所有样式文件并尝试运行它。但它仍然不起作用,显示与上述相同的输出。

标签: android android-studio android-fragments android-alertdialog android-dialogfragment


【解决方案1】:

尝试改变:

        Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout);

到这里:

    Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.setContentView(layout);

已添加 1 个

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

这个用于保持对话框周围的背景透明。


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

并且这个保持你的对话框高度就像它在 XML 文件中设置的一样。

【讨论】:

  • 按照您所说的进行更改后,它现在显示了预期的布局。正如您所说,dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 方法应该确保我的警报只占用布局文件中提到的那么多空间。但是警报对话框仍然占据整个屏幕。我尝试使用该 setLaout() 方法进行调整,但它似乎不起作用。有什么建议可以解决整个屏幕的警报对话框吗?
  • @samriddha 正如您在我的代码中看到的那样,对话框替换了整个窗口,因此我们没有其他方法可以只占用布局文件中的空间。
  • 如果您希望能够在对话框外单击,您必须为对话框的窗口设置标志 FLAG_WATCH_OUTSIDE_TOUCH。通过使用这个: dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
  • 非常感谢您的帮助。我通过将自定义对话框类的父类从 DialogFragment 更改为 Dialog 解决了高度问题。之后添加以下行即可:getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 谢谢再次交配。
  • 我很高兴听到这个消息!
猜你喜欢
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
相关资源
最近更新 更多