【发布时间】:2019-03-15 03:36:55
【问题描述】:
我有一个布局,其中 TextView(可变宽度)需要能够在父级的完整边界上水平移动(基于数据和视图事件)。
为了实现这一目标,我所做的就是使用带有指南的 ConstraintLayout。该指南有一个 app:layout_constraintGuide_percent 约束,我以编程方式在 0 和 1 之间进行调整,并且 TextView 被 layout_constraintEnd_toEndOf 和 layout_constraintStart_toStartOf 约束到该指南。
这里的问题是,当该准则的 percent 接近 0 或 1(在我的情况下分别约为 0.05 和 0.95)时,TextView 开始消失在视图边界之外。
在 iOS 中,我会创建一个更高优先级的约束,将 TextView 限制在父级的范围内,但使用 Android ConstraintLayout 似乎不太可能?
完整的布局文件:
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:paddingTop="20dp"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="@+id/guideline"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
tools:text="Variable text!"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintTop_toBottomOf="@id/textview"
/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
-
文本到达末尾会发生什么?对于
TextView来说,两侧是否应该是一个硬停止,这样即使指南靠近边缘,它也不会前进?
标签: android android-constraintlayout