当根视图是 ScrollView 时,如何正确地使 ConstraintLayout 内的视图占据可见区域的百分比高度
How to properly make a view inside ConstraintLayout take percent height of visible area when root view is a ScrollView
我有这个视图层次结构
ScrollView
ConstraintLayout
ImageView
LinearLayout
TextView
TextView
TextView
...
我想要实现的是 ImageView 垂直占据可见屏幕的 65%...但是由于根视图是 ScrollView,ImageView 占据可滚动区域的 65%...我想要ImageView 占据了可见区域的 65%。
为了得到这样的东西,最好的处理方法是什么?
源代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/rain"
app:layout_constraintHeight_percent=".65"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/header">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AAAAAA"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BBBB"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CCCCC"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DDDDD"
android:textSize="50dp"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</ScrollView>
提前致谢
在滚动视图中不能将高度设置为百分比。
我建议您使用宽高比来满足您的要求。
在您的图像视图中设置高度 0dp 并设置高度的尺寸比例。
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio
我有这个视图层次结构
ScrollView
ConstraintLayout
ImageView
LinearLayout
TextView
TextView
TextView
...
我想要实现的是 ImageView 垂直占据可见屏幕的 65%...但是由于根视图是 ScrollView,ImageView 占据可滚动区域的 65%...我想要ImageView 占据了可见区域的 65%。
为了得到这样的东西,最好的处理方法是什么?
源代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/rain"
app:layout_constraintHeight_percent=".65"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/header">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="AAAAAA"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BBBB"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CCCCC"
android:textSize="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DDDDD"
android:textSize="50dp"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</ScrollView>
提前致谢
在滚动视图中不能将高度设置为百分比。
我建议您使用宽高比来满足您的要求。
在您的图像视图中设置高度 0dp 并设置高度的尺寸比例。
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio