Android 在BottomSheetDialog片段中将视图固定到屏幕底部
我在我的项目中使用了BottomSheetDialogFragment,我将其设计如下: 目标:我要将BottomSheetDialog的底部菜单粘贴到屏幕底部,无论是折叠还是展开模式 因此,在BottomSheetDialog布局中,我使用RelativeLayout作为父对象,使用“layout_alignParentBottom”作为菜单容器,如下所示:Android 在BottomSheetDialog片段中将视图固定到屏幕底部,android,modal-dialog,bottom-sheet,Android,Modal Dialog,Bottom Sheet,我在我的项目中使用了BottomSheetDialogFragment,我将其设计如下: 目标:我要将BottomSheetDialog的底部菜单粘贴到屏幕底部,无论是折叠还是展开模式 因此,在BottomSheetDialog布局中,我使用RelativeLayout作为父对象,使用“layout_alignParentBottom”作为菜单容器,如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/re
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
tools:context=".MyBottomSheetDialogFragment">
<RelativeLayout
android:id="@+id/topSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
....
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/descriptionContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topSection">
....
</android.support.v4.widget.NestedScrollView>
<HorizontalScrollView
android:id="@+id/iconsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
....
</HorizontalScrollView>
</RelativeLayout>
....
正如您所看到的,底部菜单一开始是不可见的
有人能帮我解决这个问题吗
正如您所看到的,底部菜单一开始是不可见的
有人能帮我解决这个问题吗
我猜这个行为工作得很好,因为您将NestedScrollView
(中心内容)的layout\u height
设置为wrap\u content
,也就是说,它将被内部内容包装
同时,
android:layout_alignParentBottom="true"
到HorizontalScrollView
(在layout
下面)意味着它将位于当前的另一个layout
s下强>
因此,如果您试图查看它是否正常工作,请设置100dp
-50dp
(或显示BottomSheetDialog
时可以看到的特定大小)您可能会看到下面的layout
和其他layout
将可见,而不是wrap\u content
到NestedScrollView
不管怎么说,这个布局中的所有内容看起来都是正确的。图片说明了真相。为了解决这个问题,我在尝试时想到了一些事情,但没有成功
但我最终还是这样解决了:
对于折叠模式,我将bottomSheetBehavior的Peek高度设置为屏幕的1/3(使用以下代码):
所以我决定这样做:
1-对于折叠模式:底部板材容器的高度=底部板材行为的高度
2-对于展开模式:底板容器的高度=全屏高度
因此,我编写了以下代码(完整代码):
WordDetailsBottomSheet.java
word\u details\u bottom\u sheet.xml
....
将重力设置为底部如何?你还没试过吗?此外,正如我所看到的,在布局顶部有一个RelativeLayout
可以是LinearLayout
-FrameLayout
的视图,您可以轻松管理下面的其他布局。你可能想看看:@1139;ઽ૯ท 我做到了,但事实上,问题是,我要在对话中做到这一点!如果你看这张图片,你会发现原因是什么:这更像是一个定制的BottomSheetDialogFragment
,而不是我在回答中提到的BottomSheetDialog
的问题。
View bottomSheetContainer = dialog.findViewById(R.id.bottomSheetContainer);
View parent = (View) bottomSheetContainer.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) params.getBehavior();
View inflatedView = View.inflate(getContext(), R.layout.word_details_bottom_sheet, null);
inflatedView.measure(0, 0);
int screenHeight = getActivity().getResources().getDisplayMetrics().heightPixels;
if (bottomSheetBehavior != null) {
bottomSheetBehavior.setPeekHeight(screenHeight /3);
}
public class WordDetailsBottomSheet extends BottomSheetDialogFragment {
public WordDetailsBottomSheet() { // Required empty public constructor }
@NotNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
BottomSheetDialog dialog = new BottomSheetDialog(getActivity(), 0);
dialog.setContentView(R.layout.word_details_bottom_sheet);
View bottomSheetContainer = dialog.findViewById(R.id.bottomSheetContainer);
View parent = (View) bottomSheetContainer.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) params.getBehavior();
View inflatedView = View.inflate(getContext(), R.layout.word_details_bottom_sheet, null);
inflatedView.measure(0, 0);
int screenHeight = getActivity().getResources().getDisplayMetrics().heightPixels;
int statusBarHeight = getStatusBarHeight();
if (bottomSheetBehavior != null) {
bottomSheetBehavior.setPeekHeight(screenHeight / BOTTOM_SHEET_PEEK_HEIGHT_PERCENT);
bottomSheetContainer.getLayoutParams().height = bottomSheetBehavior.getPeekHeight();
}
bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View view, int newState) {
switch (newState) {
case BottomSheetBehavior.STATE_EXPANDED:
bottomSheetContainer.getLayoutParams().height = screenHeight-statusBarHeight;
break;
case BottomSheetBehavior.STATE_COLLAPSED:
bottomSheetContainer.getLayoutParams().height = bottomSheetBehavior.getPeekHeight();
break;
case BottomSheetBehavior.STATE_HIDDEN:
dismiss();
break;
default:
break;
}
}
@Override
public void onSlide(@NonNull View view, float slideOffset) {
}
});
return dialog;
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomSheetContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
tools:context=".MyBottomSheetDialogFragment">
<RelativeLayout
android:id="@+id/topSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
....
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/descriptionContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topSection">
....
</android.support.v4.widget.NestedScrollView>
<HorizontalScrollView
android:id="@+id/iconsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
....
</HorizontalScrollView>
</RelativeLayout>