Android ConstraintLayout: 如何将控件中心点定位到另一个控件的右上角
要将一个控件的中心点位于另一个控件的右上角,可以使用ConstraintLayout中的约束属性进行布局。以下是一个示例代码:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/centeredView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Centered View"
app:layout_constraintTop_toTopOf="@id/anchorView"
app:layout_constraintRight_toRightOf="@id/anchorView"
app:layout_constraintBottom_toBottomOf="@id/anchorView"
app:layout_constraintLeft_toLeftOf="@id/anchorView"/>
<TextView
android:id="@+id/anchorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Anchor View"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:padding="16dp"/>
</android.support.constraint.ConstraintLayout>
在这个例子中,TextView centeredView 的中心点被约束在了 anchorView 的右上角。centeredView 的顶部约束设置为和 anchorView 的顶部对齐,右侧约束设置为和 anchorView 的右侧对齐,底部约束设置为和 anchorView 的底部对齐,左侧约束设置为和 anchorView 的左侧对齐。
这样就可以实现将 centeredView 的中心点位于 anchorView 的右上角。
原文地址: https://www.cveoy.top/t/topic/p66K 著作权归作者所有。请勿转载和采集!