我需要实施BottomSheetDialogFragment
并面对这个问题。我需要我BottomSheetDialogFragment
有固定的高度。有谁知道怎么做?
这是我的片段内容的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_height"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/drag_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#FF0000"
android:text="Title"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/white"
android:layout_weight="1"/>
<TextView
android:id="@+id/ok_button"
android:layout_width="match_parent"
android:layout_height="54dp"
android:background="@android:color/holo_blue_dark"
android:gravity="center"
android:text="Hello"
android:textColor="@android:color/white"
android:textSize="24sp"/>
</LinearLayout>
在setupDialog()
我正在这样做:
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
dialog.setContentView(contentView);
CoordinatorLayout.LayoutParams layoutParams = ((CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams());
CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
if (behavior != null && behavior instanceof BottomSheetBehavior) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(bottomSheetCallback);
((BottomSheetBehavior) behavior).setPeekHeight(getResources().getDimensionPixelSize(R.dimen.bottom_sheet_height));
}
initRecyclerView(contentView);
}
行为很常见:
private BottomSheetBehavior.BottomSheetCallback bottomSheetCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
UPD:通过将固定高度设置为 RecyclerView 解决。有谁知道更好的方法?