eig*_*ght 2 android android-layout android-constraintlayout
我得到了这个设置:
<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>
Run Code Online (Sandbox Code Playgroud)
现在,当视图未满并且不需要滚动条时 - 一切都按预期工作 - 图像相对于屏幕尺寸为 35%。但随着图像下方出现更多内容,就出现了对滚动条的需求,并且指南的 0.35% 的 constrainGuide 似乎是根据屏幕的整个长度(而不是物理长度)计算的,因此随着视图变得“更长”,ImageView 也会变得更大。有没有办法避免这种情况并且始终具有物理屏幕尺寸的 x%?
您指定的参考线放置在距离 顶部一定百分比的位置ConstraintLayout
。不幸的是,对于您的应用程序,指南与视图的整体高度相关,而不是与屏幕的百分比相关。因此,如果ConstraintLayout
高于分配给它的屏幕尺寸,您将看到这种变化。请参阅的文档Guideline
。
可以通过三种不同的方式定位指南:
- 指定距布局左侧或顶部的固定距离 (layout_constraintGuide_begin)
- 指定距布局右侧或底部的固定距离 (layout_constraintGuide_end)
- 指定布局宽度或高度的百分比 (layout_constraintGuide_percent)
您可以根据 指定距布局顶部的静态偏移量dp
,但这不能适应不同的屏幕尺寸。我不相信仅仅使用 XML 就有解决方案。
但是,您可以计算代码中的像素数并在运行时设置距离。您需要将 更改Guideline
为距布局顶部固定距离的距离,计算距顶部的距离,然后调用setGuidelineBegin
以放置辅助线。
设置指导线开始
void setGuidelineBegin (intGuidelineID,intmargin)
设置引导线距上边缘或左边缘的距离。