我有一个BottomSheetDialogFragment
,我希望能在任何屏幕上显示它。我已经花了一天的时间尝试编程更改该表的peek高度,但似乎没有任何变化。
这是我的布局,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之类的值,当我显示表格时仍然没有任何变化。