我有一个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
。这是因为当我在不同屏幕上显示底部工作表时,我已使内容可自定义,因此我通过代码将自定义布局加载到其中。
这是我的片段类:
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);
}
}
}
}
然后,在我想要显示底页的地方,做:
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>
将导致
以编程方式设置偷看高度
首先,您必须将底页放置在这样的坐标布局中
<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);
}