Android 如何设置底部板材窥视高度,以便最初仅显示某些零件?
假设我的底部工作表有如下几行小部件。如果我最初只想显示前两行(即前两行Android 如何设置底部板材窥视高度,以便最初仅显示某些零件?,android,android-layout,android-widget,bottom-sheet,Android,Android Layout,Android Widget,Bottom Sheet,假设我的底部工作表有如下几行小部件。如果我最初只想显示前两行(即前两行LinearLayouts),但不想显示下面其余的小部件。我不想一开始就看到这些。如何设置正确的窥视高度?硬编码app:behavior\u peek height可能无法工作,因此我需要通过编程进行设置,但如何计算高度 还是有更推荐的方法来获得相同的结果?我的意思是,如果我测试谷歌地图,长按一个位置首先只显示标题部分作为底页,但当我尝试向上滚动底页时,感觉好像标题部分(可能不是真正的底页)被包含所有元素的真正底页所取代。如果
LinearLayout
s),但不想显示下面其余的小部件。我不想一开始就看到这些。如何设置正确的窥视高度?硬编码app:behavior\u peek height
可能无法工作,因此我需要通过编程进行设置,但如何计算高度
还是有更推荐的方法来获得相同的结果?我的意思是,如果我测试谷歌地图,长按一个位置首先只显示标题部分作为底页,但当我尝试向上滚动底页时,感觉好像标题部分(可能不是真正的底页)被包含所有元素的真正底页所取代。如果我的解释还不够,请自己试试谷歌地图
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView/>
<android.support.v7.widget.AppCompatSpinner/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView/>
<TextView/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView/>
<TextView/>
</LinearLayout>
<android.support.v7.widget.RecyclerView/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
我会使用一个
ViewTreeObserver.OnGlobalLayoutListener来解决这个问题,等待底部板材的布局,然后调用BottomSheetBehavior.setPeekHeight()
,使用您不想看到的第一个视图的y坐标
private BottomSheetBehavior<View> behavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bottomSheet = findViewById(R.id.bottomSheet);
behavior = BottomSheetBehavior.from(bottomSheet);
final LinearLayout inner = findViewById(R.id.inner);
inner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
inner.getViewTreeObserver().removeOnGlobalLayoutListener(this);
View hidden = inner.getChildAt(2);
behavior.setPeekHeight(hidden.getTop());
}
});
}
私人行为;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
视图底页=findViewById(R.id.底页);
行为=底部板材行为。从(底部板材);
最终线性布局内部=findViewById(R.id.inner);
内部.getViewTreeObserver().addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener()){
@凌驾
公共图书馆{
getViewTreeObserver().removeOnGlobalLayoutListener(此);
视图隐藏=内部.getChildAt(2);
behavior.setpeek高度(hidden.getTop());
}
});
}
在本例中,我的底部工作表是一个NestedScrollView
,其中包含一个LinearLayout
,其中包含许多TextView
s。通过将窥视高度设置为第三个TextView
(由getChildAt(2)
获得)的顶部,我的底页在折叠时正好显示两个TextView
定制@Ben p.的答案,将视图id作为Peek Height
的参考,并生成一个函数:
/**
* Gets the bottom part of the target view and sets it as the peek height of the specified @{BottomSheetBehavior}
*
* @param layout - layout of the bottom sheet.
* @param targetViewId - id of the target view. Must be a view inside the 'layout' param.
* @param behavior - bottom sheet behavior recipient.
*/
private fun <T : ViewGroup> getViewBottomHeight(layout: ViewGroup,
targetViewId: Int,
behavior: BottomSheetBehavior<T>) {
layout.apply {
viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver.removeOnGlobalLayoutListener(this)
behavior.peekHeight = findViewById<View>(targetViewId).bottom
}
})
}
}
/**
*获取目标视图的底部,并将其设置为指定@{BottomSheetBehavior}的peek高度
*
*@param layout-底页的布局。
*@param targetViewId-目标视图的id。必须是“布局”参数内的视图。
*@param behavior-底部工作表行为收件人。
*/
private fun getViewBottomHeight(布局:视图组,
targetViewId:Int,
行为:行为){
布局。应用{
viewTreeObserver.addOnGlobalLayoutListener(
对象:ViewTreeObserver.OnGlobalLayoutListener{
覆盖Loballayout(){
viewTreeObserver.removeOnGlobalLayoutListener(此)
behavior.peek高度=findViewById(targetViewId)。底部
}
})
}
}
在我们的用例中,我们需要定位视图的底部部分,因此我们将其设置为那样。它可以根据使用情况进行调整。我可以获取此代码的布局文件吗?如果可能,还可以获取代码。。我试图将peekheight设置为与我拖动底板的位置相同。但我找不到当前拖动位置的值。