【发布时间】:2019-06-23 20:53:54
【问题描述】:
问题
我在 ConstraintLayout 中有一个 TextView。考虑 TextView 的高度为 20dp,我希望它位于 ConstraintLayout 的顶部,边距为 50dp。但是,ConstraintLayout 的高度不是固定的,可能小于 70dp,这意味着 TextView 可能不适合其中。在这种情况下,我想将 TextView 垂直集中在 ConstraintLayout 中。
我的方法
我尝试了以下布局:
<ConstraintLayout android:layout_height="match_parent">
<TextView
android:layout_height="20dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp"
app:layout_constraintVertical_bias="0.0"/>
</ConstraintLayout>
当 ConstraintLayout 中有足够的空间容纳 TextView 和上边距时,此布局效果很好。但是如果没有,我需要将垂直偏差更改为 0.5,以便将 TextView 集中在 ConstraintLayout 中。
因此,是否可以根据是否有足够的空间来满足所有约束来分配不同的偏差(在 XML 中或以编程方式)?否则,还有什么其他方法可以使用?
【问题讨论】:
标签: android android-layout android-constraintlayout