我正在使用我的应用程序中的底页。
<include
android:layout_height="wrap_content"
android:layout_width="wrap_content"
layout="@layout/layout_book"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_peekHeight="0dp"
/>
这是我最下面的工作表,当我将peek height从这个xml更改为120dp时,我得到了正确的视图。但是,当尝试在java中执行相同的操作时,我得到了不同的结果,即高度比xml的要小。
bottomSheetBehavior.setPeekHeight(120);
理想情况下,它不应该是这样的。我对此一无所知。谢谢。
发布于 2018-10-29 03:23:08
setPeekHeight()
(和许多其他与大小/尺寸相关的方法)接受像素值作为它们的参数。这意味着您需要首先将dp值转换为px。最好的方法是定义一个dimen
资源值,然后在代码中获取它:
<dimen name="peek_height">120dp</dimen>
int peekHeightPx = context.getResources().getDimensionPixelSize(R.dimen.peek_height);
bottomSheetBehavior.setPeekHeight(peekHeightPx);
或者,您也可以使用DisplayMetrics
自己进行计算:
int peekHeightPx = (int) (context.getResources().getDisplayMetrics().density * 120);
bottomSheetBehavior.setPeekHeight(peekHeightPx);
https://stackoverflow.com/questions/53034999
复制相似问题