我希望使用百分比在父视图中定位视图,类似于CSS中的绝对定位。在我的场景中,我将有一个变量和不可预测的视图数,这些视图将以这种方式定位。
下面是一个示例,它表示具有以下位置的正方形内的3 TextViews:
1:前55%,左29%
2:前77%,左58%
3:前54%,左43%
这是否最好使用自定义绘图来完成?或者,在给定这些百分比的情况下,是否可以在特定类型的父视图中动态地定位视图?如果是前者,我如何处理文本?如果是后者,家长应该是哪种观点,我应该如何确定这些百分比呢?
发布于 2018-02-16 05:55:13
您可以使用ConstraintLayout
https://developer.android.com/training/constraint-layout/index.html
约束布局将允许您使用水平和垂直偏差根据百分比来定位视图。例如:
<android.support.constraint.ConstraintLayout
android:id="@+id/constraint"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="@+id/edition_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.33"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33"
android:background="@android:color/holo_purple"
android:text="text"/>
</android.support.constraint.ConstraintLayout>
只需确保子视图( Textview)被限制在顶部和底部,这样就可以设置垂直偏差。也可以开始,结束,这样你就可以设置水平偏差了。
其结果是:
这也可以通过编程方式完成,请参见这里的一个好指南:http://www.zoftino.com/adding-views-&-constraints-to-android-constraint-layout-programmatically
发布于 2018-02-16 05:44:11
https://stackoverflow.com/questions/48820582
复制相似问题