如何以百分比设置 Coordinatorlayout 子项的高度?
How to set height for Coordinator Layout's childs in percentage?
我想设计一个布局,由以下部分组成:布局顶部的工具栏(应占屏幕的 20%),然后是垂直下方的 ViewPager(应占屏幕的 60%) ) 然后,仍然在垂直下方,一个 BottomSheet(折叠时应占屏幕的 20%,展开时应占屏幕的 100%)。
现在我已经这样声明了 bottomsheet :
<LinearLayout
android:id="@+id/BSSECONDTOOLBAR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="???"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_gravity="bottom"
android:gravity="bottom"
android:background="#f44242" />
因为这应该是 CoordinatorLayout
的直接子代,所以我不能使用 layout_weight
属性。
我的问题是如何设置 BottomSheet
身高百分比?
您的问题有两点需要说明。首先是如何让底部sheet展开时填充父级
这很简单:设置android:layout_height="match_parent"
然后我们必须解决将底部 sheet 的 peek 高度设置为父级的 20%。这在 XML 中是不可能的,因为 CoordinatorLayout
不支持权重或百分比。因此,我们必须将它设置在Java。您可以将此代码添加到您的 onCreate()
方法中:
// assume `coordinator` is your CoordinatorLayout
coordinator.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
coordinator.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int twentyPercent = (coordinator.getHeight() / 5);
// make the toolbar 20% of the screen
View toolbar = findViewById(R.id.your_toolbar_id);
ViewGroup.LayoutParams toolbarParams = toolbar.getLayoutParams();
toolbarParams.height = twentyPercent;
toolbar.setLayoutParams(toolbarParams);
// make the viewpager the rest of the screen (60%)
View pager = findViewById(R.id.your_viewpager_id);
ViewGroup.MarginLayoutParams pagerParams = (ViewGroup.MarginLayoutParams) pager.getLayoutParams();
pagerParams.topMargin = twentyPercent;
pagerParams.height = (coordinator.getHeight() - (twentyPercent * 2));
pager.setLayoutParams(pagerParams);
// make the bottomsheet 20% of the screen
View bottomSheet = findViewById(R.id.BSSECONDTOOLBAR);
BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setPeekHeight(twentyPercent);
}
});
我知道这已经太晚了,但这个答案可能会对某人有所帮助...
我通过下面的代码实现了半屏布局
BottomSheetBehavior.from(bottom_sheet).peekHeight =
(Resources.getSystem().getDisplayMetrics().heightPixels)/2
我想覆盖任何尺寸设备的半屏,所以我除以2你可以通过改变分隔线通过上面的代码覆盖你想要的设备屏幕尺寸。
将此代码放在 activity 中,您的底部 sheet 将弹出。 (在 onCreate
方法中)
在代码中,bottom_sheet
是给予底部 sheet 根布局的 ID。
我想设计一个布局,由以下部分组成:布局顶部的工具栏(应占屏幕的 20%),然后是垂直下方的 ViewPager(应占屏幕的 60%) ) 然后,仍然在垂直下方,一个 BottomSheet(折叠时应占屏幕的 20%,展开时应占屏幕的 100%)。
现在我已经这样声明了 bottomsheet :
<LinearLayout
android:id="@+id/BSSECONDTOOLBAR"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="???"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_gravity="bottom"
android:gravity="bottom"
android:background="#f44242" />
因为这应该是 CoordinatorLayout
的直接子代,所以我不能使用 layout_weight
属性。
我的问题是如何设置 BottomSheet
身高百分比?
您的问题有两点需要说明。首先是如何让底部sheet展开时填充父级
这很简单:设置android:layout_height="match_parent"
然后我们必须解决将底部 sheet 的 peek 高度设置为父级的 20%。这在 XML 中是不可能的,因为 CoordinatorLayout
不支持权重或百分比。因此,我们必须将它设置在Java。您可以将此代码添加到您的 onCreate()
方法中:
// assume `coordinator` is your CoordinatorLayout
coordinator.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
coordinator.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int twentyPercent = (coordinator.getHeight() / 5);
// make the toolbar 20% of the screen
View toolbar = findViewById(R.id.your_toolbar_id);
ViewGroup.LayoutParams toolbarParams = toolbar.getLayoutParams();
toolbarParams.height = twentyPercent;
toolbar.setLayoutParams(toolbarParams);
// make the viewpager the rest of the screen (60%)
View pager = findViewById(R.id.your_viewpager_id);
ViewGroup.MarginLayoutParams pagerParams = (ViewGroup.MarginLayoutParams) pager.getLayoutParams();
pagerParams.topMargin = twentyPercent;
pagerParams.height = (coordinator.getHeight() - (twentyPercent * 2));
pager.setLayoutParams(pagerParams);
// make the bottomsheet 20% of the screen
View bottomSheet = findViewById(R.id.BSSECONDTOOLBAR);
BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setPeekHeight(twentyPercent);
}
});
我知道这已经太晚了,但这个答案可能会对某人有所帮助...
我通过下面的代码实现了半屏布局
BottomSheetBehavior.from(bottom_sheet).peekHeight =
(Resources.getSystem().getDisplayMetrics().heightPixels)/2
我想覆盖任何尺寸设备的半屏,所以我除以2你可以通过改变分隔线通过上面的代码覆盖你想要的设备屏幕尺寸。
将此代码放在 activity 中,您的底部 sheet 将弹出。 (在 onCreate
方法中)
在代码中,bottom_sheet
是给予底部 sheet 根布局的 ID。