ian*_*ake 40 android android-support-library android-support-design
我一直在使用支持库23.2中添加的新BottomSheetDialog ,但我想更改对话框的默认高度.我知道它可能与behavior_peekHeight控制初始高度的属性有关,但是如何BottomSheetDialog在我无法直接访问BottomSheetBehavior?时设置它?
ian*_*ake 69
您可以bottomSheetDialogTheme在Activity中设置a ,覆盖bottomSheetStyle属性behavior_peekHeight:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item>
</style>
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">@dimen/custom_peek_height</item>
</style>
Run Code Online (Sandbox Code Playgroud)
可以使用此相同的技术对其它属性,以及,如添加<item name="behavior_hideable">true</item>到AppModalStyle改变底部片材是否是可隐藏.
小智 23
你可以BottomSheetBehavior在你的代码中使用
BottomSheetDialog dialog = new BottomSheetDialog(content);
.
.
.
dialog.setContentView(view);
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) view.getParent());
mBehavior.setPeekHeight(your dialog height)
dialog.show();
Run Code Online (Sandbox Code Playgroud)
Hel*_*Csl 14
为我炒菜
override fun onStart() {
super.onStart()
dialog?.also {
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
val behavior = BottomSheetBehavior.from<View>(bottomSheet)
behavior.peekHeight = resources.displayMetrics.heightPixels //replace to whatever you want
view?.requestLayout()
}
}
Run Code Online (Sandbox Code Playgroud)
Fan*_*ang 13
styles.xml
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item>
</style>
<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">500dp</item>
</style>
BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog);
dialog.setContentView(R.layout.layout_bottom_sheet);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
iva*_*8m8 11
老实说,我不知道为什么没有人提到这些简单的方法,但是:
override fun onResume() {
super.onResume()
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
// or since com.google.android.material:material:1.1.0-beta01
(dialog as? BottomSheetDialog)?.behavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
//or
dialog.behavior.peekheight = YOUR_VALUE
Run Code Online (Sandbox Code Playgroud)
直接回答问题
问:如何直接访问BottomSheetBehavior 的高度?
答:
dialog.behavior.peekheight
我的底部工作表对话框具有回收器视图,并且没有可显示的项目,因此它正在缩小,但假设您想将窥视高度设置为 85%,无论 Recyclerview 中是否存在项目,请尝试此解决方案。
在扩展 BottomSheetDialogFragment 的类中添加以下代码
override fun onStart() {
super.onStart()
(dialog as BottomSheetDialog).behavior.peekHeight = getBottomSheetDialogDefaultHeight()
dialog?.let {
val bottomSheet = it.findViewById<View>(R.id.design_bottom_sheet)
bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
}
private fun getBottomSheetDialogDefaultHeight(): Int {
val displayMetrics = DisplayMetrics()
(context as Activity?)?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
return displayMetrics.heightPixels * 85 / 100
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是继承BottomSheetDialogFragment并控制设置内容视图的方式和时间。在视图树上,您可以得到BottomSheetDialog包装内容视图的行为。这不是一个很好的解决方案,因为它需要更多的布局遍历。重要的是,当底部状态为“状态”时,STATE_HIDDEN必须关闭对话框,否则,我们显然会违反库中提供的实现。
通过编程设置窥视高度后,必须调用内容视图requestLayout(),这实际上是另一次布局传递。
public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
setStateText(newState);
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
};
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null);
dialog.setContentView(contentView);
mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
if (mBottomSheetBehavior != null) {
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
mBottomSheetBehavior.setPeekHeight(peekHeight);
contentView.requestLayout();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32871 次 |
| 最近记录: |