繁体   English   中英

与不同的视图对齐,使它们彼此居中对齐

[英]Make alignments with different views so that they are center aligned with each other

Android Studio 3.5.3

我有以下 xml 并使用 constraintLayout 将 2 个视图和 TextView 彼此对齐。 但是,视图需要固定在 45dp 的 MarginTop。 我需要调整 textview 的 MarginTop 以使它们看起来像水平对齐。 我认为必须有更好的方法。 所以基本上,视图需要是 45dp 的 MarginTop 并且 TextView 应该看起来像水平对齐,而不必调整 MarginTop 来尝试和补偿。

     <View
        android:id="@+id/lineRight"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintEnd_toStartOf="@+id/tvOr"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_login"
        android:background="@android:color/darker_gray"/>

    <TextView
        android:id="@+id/tvOr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="34dp"
        android:fontFamily="@font/roboto_regular"
        android:text="OR"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_login" />

    <View
        android:id="@+id/lineLeft"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_marginTop="45dp"
        android:layout_marginStart="16dp"
        app:layout_constraintStart_toEndOf="@+id/tvOr"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_login"
        android:background="@android:color/darker_gray"/>

这是我试图实现的目标的图像:

在此处输入图片说明

您可以将tvOr垂直约束到其中一个视图,假设是左侧视图,然后像这样删除 marginTop:

<TextView
        android:id="@+id/tvOr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto_regular"
        android:text="OR"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/lineLeft"
        app:layout_constraintBottom_toBottomOf="@id/lineLeft"/>

这样 TextView 将与左线垂直居中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM