android - 在BottomSheetDialogFragment 中以编程方式设置查看高度

标签 android android-layout android-fragments bottom-sheet

我有一个 BottomSheetDialogFragment我希望能够在任何屏幕上显示。我花了一天时间尝试以编程方式更改工作表的透视高度,但似乎没有任何改变。

这是我的布局,bottom_sheet.xml :

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="96dp"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

你会注意到我有一个空的 NestedScrollView .这是因为当我在不同的屏幕上显示 Bottom Sheet 时,我已经使我的内容可自定义,因此我通过代码将自定义布局加载到其中。

这是我的 fragment 类:
public class BottomSheetFragment extends BottomSheetDialogFragment {

    private LinearLayout bottomSheet;
    private NestedScrollView contentView;

    public BottomSheetFragment() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.bottom_sheet, container, false);
    }

    @Override
    public void onViewCreated(@NotNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        bottomSheet = view.findViewById(R.id.bottom_sheet);
        contentView = view.findViewById(R.id.content);
    }

    // Set the layout of the NestedScrollView by passing a layout ID
    public void setLayout(int layoutId) {
        if (contentView != null) {
            contentView.removeAllViews();

            View view = getLayoutInflater().inflate(layoutId, contentView, false);

            if (view != null) {
                contentView.addView(view);
            }
        }
    }
}

然后,在我想显示 Bottom Sheet 的地方,我这样做:
BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());

我希望工作表的透视高度距离屏幕顶部 64dp。我将如何以及在哪里以编程方式执行此操作?

我还发现,即使我改变了 app:behavior_peekHeight="96dp" 的值在布局中到 500dp 之类的东西,当我显示工作表时它仍然没有改变任何东西。

最佳答案

您的bottom_sheet 中没有 View 元素。这就是为什么你在玩 peekheight 时看不到任何变化的原因.

将一些高度设置为 bottom_sheet 以查看更改。
例如,如果您设置高度(300),您将清楚地看到偷看高度生效。

例如,

<LinearLayout
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@color/colorPrimary"
            android:orientation="vertical"
            android:id="@+id/bottom_sheet"
            app:layout_behavior="@string/bottom_sheet_behavior"
            app:behavior_peekHeight="96dp"
            app:behavior_hideable="true">

            <androidx.core.widget.NestedScrollView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

将导致

enter image description here

以编程方式设置 peekheight

首先,您必须像这样将 Bottom Sheet 放在坐标布局中
<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@color/colorPrimary"
            android:orientation="vertical"
            android:id="@+id/bottom_sheet"
            app:layout_behavior="@string/bottom_sheet_behavior"
            app:behavior_peekHeight="96dp"
            app:behavior_hideable="true">

            <androidx.core.widget.NestedScrollView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

在你的类里面,你这样做。
@Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View bottomSheet = view.findViewById(R.id.bottom_sheet);
        BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setPeekHeight(150); 
    }

关于android - 在BottomSheetDialogFragment 中以编程方式设置查看高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195583/

相关文章:

android - 在 ListView 中显示 RadioGroup

android - 如何设置多个 ScrollView 大小相对于彼此动态变化?

android - 在带滑动选项卡的抽屉导航中如何为内部的每个选项卡制作和设置 fragment

java - 通过单击 ActionBar 按钮更改 FragmentPagerAdapter 中 fragment 的内容

java - JFrame 的 Android 等价物是什么?

java - Gradle 无法加载 Google playServicesVersion

android - ScrollView如何设置多个 View ?

android - fragment 中的 startActivityForResult 不工作

android - 对话框打开动画在 Android 4.2 上不起作用

Android O 中的 android:appCategory。哪些值?