pro*_*m85 43 android android-support-library androiddesignsupport bottom-sheet
我创建了一个BottomSheetDialogFragment
,我想调整它的最大扩展高度.我怎样才能做到这一点?我可以找回BottomSheetBehaviour
但是我能找到的只是一个用于窥视高度的二传手,但是对于扩展的高度没什么.
public class DialogMediaDetails extends BottomSheetDialogFragment
{
@Override
public void setupDialog(Dialog dialog, int style)
{
super.setupDialog(dialog, style);
View view = View.inflate(getContext(), R.layout.dialog_media_details, null);
dialog.setContentView(view);
...
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setPeekHeight(...);
// how to set maximum expanded height???? Or a minimum top offset?
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
我为什么需要那个?因为我BottomSheet
在一个全屏活动中显示一个Dialog,如果BottomSheet
在顶部留下一个空间,它看起来很糟糕......
Rub*_*Yoo 24
高度被包装,因为膨胀的视图被添加到FrameLayout中layout_height=wrap_content
.请参阅https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_sheet_dialog.xml中的 FrameLayout(R.id.design_bottom_sheet).
下面的类使得底部工作表全屏,背景透明并完全展开到顶部.
public class FullScreenBottomSheetDialogFragment extends BottomSheetDialogFragment {
@CallSuper
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
}
View view = getView();
view.post(() -> {
View parent = (View) view.getParent();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
CoordinatorLayout.Behavior behavior = params.getBehavior();
BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT)
});
}
}
Run Code Online (Sandbox Code Playgroud)
---编辑2018年8月30日---一年后我意识到背景是错误的.当用户拖动对话框时,这会将背景与内容一起拖动.我修复了它,以便底部工作表的父视图被着色.
小智 21
我找到了一个更简单的答案; 在您的示例中,您使用此代码获取底部工作表的FrameLayout
View bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
Run Code Online (Sandbox Code Playgroud)
然后,您可以将该视图的布局参数上的高度设置为您要将展开高度设置为的任何高度.
bottomSheet.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
Run Code Online (Sandbox Code Playgroud)
Mub*_*rak 19
它对我有用。在BottomSheetDialogFragment 的onViewCreated() 方法上添加代码
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val dialog = dialog as BottomSheetDialog
val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?
val behavior = BottomSheetBehavior.from(bottomSheet!!)
behavior.state = BottomSheetBehavior.STATE_EXPANDED
val newHeight = activity?.window?.decorView?.measuredHeight
val viewGroupLayoutParams = bottomSheet.layoutParams
viewGroupLayoutParams.height = newHeight ?: 0
bottomSheet.layoutParams = viewGroupLayoutParams
}
})
dialogView = view
}
Run Code Online (Sandbox Code Playgroud)
不要忘记删除viewTreeObserver。
override fun onDestroyView() {
dialogView?.viewTreeObserver?.addOnGlobalLayoutListener(null)
super.onDestroyView()
}
Run Code Online (Sandbox Code Playgroud)
Mig*_*ieC 14
大更新 避免重复的代码我给出了一个完整答案的链接,在那里你可以找到有关如何获得谷歌地图等完整行为的所有解释.
我想调整它的最大扩展高度.我怎样才能做到这一点?
两者BottomSheet
并BottomSheetDialogFragment
用BottomSheetBehavior,你可以在支持库23.x发现
该Java类有两种不同的用途mMinOffset
,其中一种用于定义它将用于绘制其内容的父级区域(可能是a NestedScrollView
).而另一个用途是,如果你定义扩展的锚点,我的意思是,如果你向上滑动它STATE_COLLAPSED
会激活你的BottomSheet
直到他到达这个锚点但是如果你仍然可以保持向上滑动以覆盖所有父高度(CoordiantorLayout Height).
如果你看一下,BottomSheetDialog
你会看到这个方法:
private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
android.support.design.R.layout.design_bottom_sheet_dialog, null);
if (layoutResId != 0 && view == null) {
view = getLayoutInflater().inflate(layoutResId, coordinator, false);
}
FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
if (params == null) {
bottomSheet.addView(view);
} else {
bottomSheet.addView(view, params);
}
// We treat the CoordinatorLayout as outside the dialog though it is technically inside
if (shouldWindowCloseOnTouchOutside()) {
final View finalView = view;
coordinator.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isShowing() &&
MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_UP &&
!coordinator.isPointInChildBounds(finalView,
(int) event.getX(), (int) event.getY())) {
cancel();
return true;
}
return false;
}
});
}
return coordinator;
}
Run Code Online (Sandbox Code Playgroud)
不知道你想要哪两种行为,但如果你需要第二种行为,请遵循以下步骤:
CoordinatorLayout.Behavior<V>
BottomSheetBehavior
文件复制到新文件.clampViewPositionVertical
使用以下代码修改方法:
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return constrain(top, mMinOffset, mHideable ? mParentHeight : mMaxOffset);
}
int constrain(int amount, int low, int high) {
return amount < low ? low : (amount > high ? high : amount);
}
Run Code Online (Sandbox Code Playgroud)添加新状态
public static final int STATE_ANCHOR_POINT = X;
Run Code Online (Sandbox Code Playgroud)修改下一个方法:onLayoutChild
,onStopNestedScroll
,BottomSheetBehavior<V> from(V view)
和setState
(可选)
以下是它的外观:
[]
aps*_*mer 12
获取工作表行为的参考,
private val behavior by lazy { (dialog as BottomSheetDialog).behavior }
Run Code Online (Sandbox Code Playgroud)
关闭fitToContents
并设置expandedOffset
为所需的像素。
behavior.isFitToContents = false
behavior.expandedOffset = 100
Run Code Online (Sandbox Code Playgroud)
科特林
就我而言,我需要定义一个固定的高度,我执行了以下操作:
val bottomSheet: View? = dialog.findViewById(R.id.design_bottom_sheet)
BottomSheetBehavior.from(bottomSheet!!).peekHeight = 250
Run Code Online (Sandbox Code Playgroud)
这样您还可以访问以下的任何BottomSheetBehavior
财产halfExpandedRatio
我建议不要使用 ids 来查找视图。BottomSheetDialogFragment
对话框中BottomSheetDialog
显示了底部工作表的行为。您可以使用它来设置窥视高度。
(dialog as BottomSheetDialog).behavior.peekHeight = ...
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41475 次 |
最近记录: |