ConstraintLayout Guideline 随view展开
ConstraintLayout Guideline expanding together with view
我得到了这个设置:
<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">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
...
</android.support.constraint.ConstraintLayout>
</ScrollView>
现在,当视图未满且不需要滚动条时 - 一切正常 - 图像占屏幕尺寸的 35%。但是随着图像下方出现更多内容,出现滚动条的需要并且指南的 constraintGuide 0.35% 似乎是根据屏幕的整个长度(不是物理长度)计算的,因此 ImageView 也随着视图变得更大 "longer".
有没有办法避免这种情况并始终保持物理屏幕尺寸的 x%?
您指定的参考线位于距离 ConstraintLayout
顶部的百分比距离处。不幸的是,对于您的应用程序,准则与视图的整体高度相关,而不是屏幕的百分比。因此,如果 ConstraintLayout
比分配给它的屏幕尺寸高,您将看到偏移。 Guideline
见 documentation。
Positioning a Guideline is possible in three different ways:
- specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)
- specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)
- specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)
您可以根据 dp
指定布局顶部的静态偏移量,但这不能适应不同的屏幕尺寸。我不相信仅使用 XML.
就有解决方案
但是,您可以在代码中计算像素数并在 run-time 基础上设置距离。您需要将 Guideline
更改为距布局顶部固定距离的那个,计算距顶部的距离,然后调用 setGuidelineBegin
放置参考线。
setGuidelineBegin
void setGuidelineBegin (int guidelineID,
int margin)
Set the guideline's distance form the top or left edge.
我得到了这个设置:
<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">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.35" />
...
</android.support.constraint.ConstraintLayout>
</ScrollView>
现在,当视图未满且不需要滚动条时 - 一切正常 - 图像占屏幕尺寸的 35%。但是随着图像下方出现更多内容,出现滚动条的需要并且指南的 constraintGuide 0.35% 似乎是根据屏幕的整个长度(不是物理长度)计算的,因此 ImageView 也随着视图变得更大 "longer". 有没有办法避免这种情况并始终保持物理屏幕尺寸的 x%?
您指定的参考线位于距离 ConstraintLayout
顶部的百分比距离处。不幸的是,对于您的应用程序,准则与视图的整体高度相关,而不是屏幕的百分比。因此,如果 ConstraintLayout
比分配给它的屏幕尺寸高,您将看到偏移。 Guideline
见 documentation。
Positioning a Guideline is possible in three different ways:
- specifying a fixed distance from the left or the top of a layout (layout_constraintGuide_begin)
- specifying a fixed distance from the right or the bottom of a layout (layout_constraintGuide_end)
- specifying a percentage of the width or the height of a layout (layout_constraintGuide_percent)
您可以根据 dp
指定布局顶部的静态偏移量,但这不能适应不同的屏幕尺寸。我不相信仅使用 XML.
但是,您可以在代码中计算像素数并在 run-time 基础上设置距离。您需要将 Guideline
更改为距布局顶部固定距离的那个,计算距顶部的距离,然后调用 setGuidelineBegin
放置参考线。
setGuidelineBegin
void setGuidelineBegin (int guidelineID, int margin)
Set the guideline's distance form the top or left edge.