假设我有一个类似的视图结构
<ConstraintLayout> <CustomViewLayout> ... ... </CustomViewLayout> </ConstraintLayout>
这是简化的,但我在底部工作表中使用上面的内容,我有时会根据我的CustomViewLayout高度更改ConstraintLayout的高度和底部工作表的峰值高度.我的问题是,如果CustomViewLayout的一部分被切断,那就是 – 它有点在屏幕之外,因为ConstraintLayout不够高 – 我不再能够获得正确的“全高”.在这种情况下,我总是只看到视图屏幕上的可见部分.
那么如何才能获得部分偏离屏幕的视图的全高?
谢谢!
编辑:
我应该补充说,我尝试过的是globalLayoutListener,以及customViewLayout.post {}(当然还有通常的customViewLayout.height).但是当视图的一部分位于屏幕之外时,这些都不会测量整个高度.
解决方法
为了检查我之前的答案,我开发了一个测试项目来检查确切的情况.当它的某些部分位于屏幕之外时,测量布局高度是成功的(布局高度测量为4231px,屏幕高度为1920px).如果视图足够长并且屏幕未完全显示,则应将其放在可滚动视图中.因此,我将customViewLayout放在NestedScrollView中,以便在底部工作表展开后使用剩余滚动量.这是代码和结果:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <android.support.constraint.ConstraintLayout android:id="@+id/layoutBottomSheet" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="android.support.design.widget.BottomSheetBehavior"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/customViewLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/colorAccent"> <TextView android:id="@+id/stateTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:layout_marginTop="16dp" android:gravity="center_horizontal"/> <TextView android:id="@+id/sizeTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:padding="24dp" android:gravity="center_horizontal"/> <TextView android:id="@+id/contentTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:padding="24dp" android:text="@string/lorem" android:gravity="center_horizontal"/> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.constraint.ConstraintLayout> </android.support.design.widget.CoordinatorLayout>
主要活动:
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setSupportActionBar(toolbar) customViewLayout.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener { override fun onGlobalLayout() { customViewLayout.viewTreeObserver.removeGlobalOnLayoutListener(this) val height = customViewLayout.measuredHeight val width = customViewLayout.measuredWidth sizeTextView.text = "Height: $height px Width: $width px" } }) val sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet) sheetBehavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { override fun onSlide(bottomSheet: View,offset: Float) { } override fun onStateChanged(bottomSheet: View,newState: Int) { when (newState) { BottomSheetBehavior.STATE_HIDDEN -> stateTextView.text = "State: Hidden" BottomSheetBehavior.STATE_EXPANDED -> stateTextView.text = "State: Expanded" BottomSheetBehavior.STATE_COLLAPSED -> stateTextView.text = "State: Collapsed" BottomSheetBehavior.STATE_DRAGGING -> stateTextView.text = "State: Dragging" BottomSheetBehavior.STATE_SETTLING -> stateTextView.text = "State: Settling" } } }) } }
屏幕: