[英]how to set height of BottomSheet full parent?
I am trying to call BottomSheet using a ndroid-support-library 23.2 on Click of button.我正在尝试在单击按钮时使用ndroid-support-library 23.2调用 BottomSheet。 Its working fine but not taking full height.它工作正常但没有达到全高。 It reside below AppBarLayout
.它位于AppBarLayout
下方。 I didn't find any solution on Android Documentation我没有在Android 文档中找到任何解决方案
This is my Screen layout这是我的屏幕布局
Here is my code.这是我的代码。
activity_main.xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="snap|enterAlwaysCollapsed"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="#5be5ad"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
<!-- BottomSheet Layout -->
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="fill_parent"
app:behavior_hideable="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java MainActivity.java
View bottomSheet;
private BottomSheetDialog mBottomSheetDialog;
private void initView(){
/*
Bottom Sheet Initialization
*/
CoordinatorLayout coordinatorLayout = (CoordinatorLayout)findViewById(R.id.main_content);
// The View with the BottomSheetBehavior
bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);
behavior = BottomSheetBehavior.from(bottomSheet);
// behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
// React to state change
CommonMethods.getInstance().e("onStateChanged", "onStateChanged:" + newState);
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
fab.setVisibility(View.GONE);
} else {
fab.setVisibility(View.VISIBLE);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
// React to dragging events
CommonMethods.getInstance().e("onSlide", "onSlide");
}
});
behavior.setPeekHeight(100);
}
Click Event Code:单击事件代码:
@Override
public void onShareClick() {
if (behavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
if(mBottomSheetDialog==null){
mBottomSheetDialog = new BottomSheetDialog(this);
View view = getLayoutInflater().inflate(R.layout.layout_bottomsheet, null);
mBottomSheetDialog.setContentView(view);
}
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
Replace代替
behavior.setPeekHeight(100);
with和
behavior.setPeekHeight(screenUtils.getHeight());
will solve your problem.将解决您的问题。
public class ScreenUtils {
Context ctx;
DisplayMetrics metrics;
public ScreenUtils(Context ctx) {
this.ctx = ctx;
WindowManager wm = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
metrics = new DisplayMetrics();
display.getMetrics(metrics);
}
public int getHeight() {
return metrics.heightPixels;
}
public int getWidth() {
return metrics.widthPixels;
}
public int getRealHeight() {
return metrics.heightPixels / metrics.densityDpi;
}
public int getRealWidth() {
return metrics.widthPixels / metrics.densityDpi;
}
public int getDensity() {
return metrics.densityDpi;
}
public int getScale(int picWidth) {
Display display
= ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
Double val = new Double(width) / new Double(picWidth);
val = val * 100d;
return val.intValue();
}
}
Using the above class we can set peek height like this.It will give the screen's maximum height.使用上面的类,我们可以像这样设置窥视高度。它将给出屏幕的最大高度。
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) contentView.getParent());
ScreenUtils screenUtils=new ScreenUtils(getActivity());
mBehavior.setPeekHeight(screenUtils.getHeight());
don't use BottomSheetDialog
just use default behaviour like below(remove BottomSheetDialog
code)不要使用BottomSheetDialog
只需使用如下所示的默认行为(删除BottomSheetDialog
代码)
@Override
public void onShareClick() {
if (behavior.getState() == BottomSheetBehavior.STATE_HIDDEN || behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
} else {
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
use BottomSheet
layout to match_parent
使用BottomSheet
布局match_parent
<FrameLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
</FrameLayout>
by above changes you have see BottomSheet with fullscreen通过上述更改,您可以全屏看到BottomSheet
For BottomSheetDialog you need to set fitContents to false对于BottomSheetDialog,您需要将fitContents 设置为false
behavior.isFitToContents=false
behavior.state=BottomSheetBehavior.STATE_EXPANDED
只需添加这个(Kotlin):
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
Well, this question is repeated soooo many times in Stackoverflow and noone got the answer.好吧,这个问题在 Stackoverflow 中重复了很多次,但没有人得到答案。 But after some of experiment, I got the solution guys:但是经过一些实验,我得到了解决方案:
class SortProductBottomSheet : BaseBottomSheet() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View = inflater.inflate(R.layout.dialog_blablabla, container, false)-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//THIS IS OUR LIFESAVER
val dm = Resources.getSystem().displayMetrics
val rect = dm.run { Rect(0, 0, heightPixels, widthPixels) }
view.minimumHeight = rect.height()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.