Skip to content

DialogFragment自适应高度

我们用DialogFragment来显示一些信息。 dialog的高度会随着显示的信息的多少而变化。

下面给出几个例子

4个layout

先准备一些文字资源

<string name="sample_text2">欢迎来看我的工程</string>

不同的layout,显示同样多的控件

ConstraintLayout

show_version_dialog_con.xml 显示几个TextView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:minWidth="200dp"
    android:orientation="vertical"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:paddingBottom="12dp">

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Title"
        android:textColor="#120101"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:textColor="#222222"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title_tv" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sample_text2"
        android:textColor="#222222"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv1" />

    <Button
        android:id="@+id/confirm_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:text="OK"
        android:textColor="#222222"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv2" />

</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintLayout button靠底

相比于上一个,我们把Button的位置改为在父layout的底部。 show_version_dialog_con_btn_bot.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:minWidth="200dp"
    android:orientation="vertical"
    android:paddingStart="0dp"
    android:paddingEnd="0dp"
    android:paddingBottom="12dp">

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Title"
        android:textColor="#120101"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:textColor="#222222"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/title_tv" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sample_text2"
        android:textColor="#222222"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv1" />

    <Button
        android:id="@+id/confirm_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:text="OK"
        android:textColor="#222222"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

最后一个Button设置了app:layout_constraintBottom_toBottomOf="parent"

RelativeLayout

show_version_dialog_rela.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="#DADADA">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="我是标题" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:textColor="#007CFF" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv1"
        android:layout_centerHorizontal="true"
        android:text="@string/sample_text2"
        android:textColor="#007CFF" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv2"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:text="yes" />

</RelativeLayout>

RelativeLayout button靠底

show_version_dialog_rela_btn_bot.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="#DADADA">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="我是标题" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:textColor="#007CFF" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv1"
        android:layout_centerHorizontal="true"
        android:text="@string/sample_text2"
        android:textColor="#007CFF" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="20dp"
        android:text="yes" />

</RelativeLayout>
最后一个Button设置了android:layout_alignParentBottom="true"

DialogFragment的代码

显示一些系统基础信息。

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

public class ShowVersionDialogFrag extends DialogFragment {
    private static final String TAG = "rustAppShowVersion";
    public int layoutId = R.layout.show_version_dialog_con;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView");
        return inflater.inflate(layoutId, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d(TAG, "onViewCreated");
        TextView tv1 = view.findViewById(R.id.tv1);
        final String text1 = "RustFisher \nSDK_INT: " + Build.VERSION.SDK_INT +
                "\nBuild.MODEL: " + Build.MODEL +
                "\nBuild.PRODUCT: " + Build.PRODUCT +
                "\nBuild.BRAND: " + Build.BRAND;
        tv1.setText(text1);
    }
}

使用的时候把layout的id传进去

private void popShowVersionDialog(int layoutId) {
    ShowVersionDialogFrag dialogFrag = new ShowVersionDialogFrag();
    dialogFrag.layoutId = layoutId;
    dialogFrag.show(getSupportFragmentManager(), "show-dialog-for-bug");
}

运行效果图对比

可以看出,使用RelativeLayout时候,如果有控件靠着底部,会把dialog拉高。

作者: RustFisher
联系: rf.cs@foxmail.com
博客: rustfisher.com | RustFisher cnblog
示例: AndroidTutorial Gitee, Tutorial Github
链接: https://www.an.rustfisher.com/android/fragment/dialogFragment/df-dialog-height-changed/
一家之言,仅当抛砖引玉。如有错漏,还请指出。