gpt4 book ai didi

android - 如何实现固定高度的 BottomSheetDialogFragment

转载 作者:太空宇宙 更新时间:2023-11-03 12:15:29 28 4
gpt4 key购买 nike

我需要实现 BottomSheetDialogFragment 并面对这个问题。我需要我的 BottomSheetDialogFragment 具有固定高度。有人知道怎么做吗?

这是我的 fragment 内容的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 解决。有谁知道更好的方法?

最佳答案

可以直接通过Creating it style给出固定高度。

styles.xml

<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">500dp</item>
</style>

更新:

BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
dialog.setContentView(R.layout.layout_bottom_sheet);
dialog.show();

或第二种方法:

 CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();

if( behavior != null && behavior instanceof BottomSheetBehavior ) {
((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
((BottomSheetBehavior) behavior).setPeekHeight(300);
}

关于android - 如何实现固定高度的 BottomSheetDialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38266244/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com