首先,先来两个图
设置了列表最大高度的效果
设置了列表最小高度的效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/09b9832d4e7a44ef9e5d0a52b94da3a1.png
这里是使用DialogFragment实现的,布局使用了LinearLayout包裹ConstraintLayout实现的效果
布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clDialogFragOrderOTATitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:background="@drawable/shape_white_top_16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="98dp"
android:gravity="center"
android:text="@string/orderOTATitleText"
android:textColor="@color/colorText323233"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDialogFragOrderOTAClose"
android:layout_width="wrap_content"
android:layout_height="98dp"
android:layout_marginEnd="30dp"
android:gravity="center"
android:paddingStart="10dp"
android:paddingEnd="4dp"
android:text="@string/updatePriceCloseText"
android:textColor="@color/colorText969799"
android:textSize="32sp"
app:layout_constraintBottom_toTopOf="@+id/rlDialogFragOrderOTA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:id="@+id/rlDialogFragOrderOTA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@+id/btDialogFragOrderOTA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="800dp"
app:layout_constraintHeight_min="260dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvDialogFragOrderOTAClose">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvDialogFragOrderOTA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:overScrollMode="never"
tools:listitem="@layout/item_rv_order_ota" />
</RelativeLayout>
<Button
android:id="@+id/btDialogFragOrderOTA"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="16dp"
android:background="@drawable/shape_orange_100dp"
android:stateListAnimator="@null"
android:text="@string/confirmText"
android:textColor="@color/white"
android:textSize="32sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
DialogFragment中的主要代码
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, R.style.fullScreenDialogStyle)
}
override fun onStart() {
super.onStart()
dialog?.window?.apply {
setWindowAnimations(R.style.DownToUp)
attributes = attributes.apply {
width = ViewGroup.LayoutParams.MATCH_PARENT
//这里一定设置成全屏,并且配合Styele把dialogFragment设置成全屏无标题
height = ViewGroup.LayoutParams.MATCH_PARENT
//如果需要状态栏显示的,则不使用Style,并且动态设置height
//如果不动态设置,这里会出现状态栏黑色的问题
//动态设置了全屏高度会有一个小问题,布局中的View会向下偏移大概50dp的高度
//这里需要自行调整xml中View的距离底部的高度
//height = ConverScreenUtil.getScreenHeight()
gravity = Gravity.BOTTOM
}
}
}
<style name="fullScreenDialogStyle" parent="android:style/Theme.Dialog">
<!--我这里设置了Dialog的全屏无标题,不显示状态栏-->
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
如果出现了DilaogFragment闪屏的问题,可以参考以下代码
@CallSuper
@Override
public void onStart() {
Dialog dialog = getDialog();
Window window = null;
if (dialog != null) {
window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
);
}
}
super.onStart();
if (window != null) {
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
}
Over,完毕!