在ImageView中水平居中TextView,Imageview旁边还有另一个视图

时间:2018-02-05 16:09:23

标签: android android-layout android-relativelayout

您好,我想按如下方式构建UI enter image description here

我想将imageview置于textview上方,同时在imagview虚线旁边还有另一个视图我无法做到这一点。

目前我正在使用RelativeLayout。知道如何做到这一点?

以下是XML

  <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <ImageView
            android:id="@+id/ivnumberfill"
            android:layout_width="16dp"
            android:layout_height="16dp"
            app:srcCompat="@drawable/ic_otp_fill" />

        <ImageView
            android:id="@+id/ivfirstdiv"
            android:layout_width="76dp"
            android:layout_height="2dp"
            android:layout_alignBottom="@id/ivnumberfill"
            android:layout_alignTop="@+id/ivnumberfill"
            android:layout_toRightOf="@id/ivnumberfill"
            android:src="@drawable/stroke_divider" />

        <ImageView
            android:id="@+id/ivotp"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_toRightOf="@+id/ivfirstdiv"
            app:srcCompat="@drawable/otp_process_circle" />

        <ImageView
            android:id="@+id/ivseconddiv"
            android:layout_width="76dp"
            android:layout_height="2dp"
            android:layout_alignBottom="@id/ivotp"
            android:layout_alignTop="@+id/ivotp"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/ivotp"
            android:background="@drawable/stroke_divider" />

        <ImageView
            android:id="@+id/ivprofile"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_toRightOf="@+id/ivseconddiv"
            app:srcCompat="@drawable/otp_process_circle" />

        <TextView
            android:id="@+id/tvnumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ivnumberfill"
            android:alig="@id/ivnumberfill"
            android:text="Mobile Number"
            android:textColor="?colorAccent"
            android:textSize="11sp" />
    </RelativeLayout>

2 个答案:

答案 0 :(得分:4)

我建议使用ConstraintLayout,您应该真正学习并使用它而不是相对布局,它更灵活,耗费更少的计算量。这是我的布局,它也更灵活 - 你可以移动指南,一切都随之移动。

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="24dp">

        <ImageView
            android:id="@+id/ivnumberfill"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_marginEnd="18dp"
            android:layout_marginTop="4dp"
            app:layout_constraintBottom_toBottomOf="@id/guideline2"
            app:layout_constraintEnd_toStartOf="@+id/ivotp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/guideline2"
            app:srcCompat="@drawable/plane" />

        <ImageView
            android:id="@+id/ivotp"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_marginEnd="18dp"
            android:layout_marginTop="4dp"
            app:layout_constraintBottom_toBottomOf="@id/guideline2"
            app:layout_constraintEnd_toStartOf="@+id/ivprofile"
            app:layout_constraintStart_toEndOf="@id/ivnumberfill"
            app:layout_constraintTop_toTopOf="@id/guideline2"
            app:srcCompat="@drawable/plane" />

        <ImageView
            android:id="@+id/ivprofile"
            android:layout_width="16dp"
            android:layout_height="16dp"
            app:layout_constraintBottom_toBottomOf="@id/guideline2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/ivotp"
            app:layout_constraintTop_toTopOf="@id/guideline2"
            app:srcCompat="@drawable/plane" />


        <ImageView
            android:id="@+id/ivfirstdiv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintWidth_default="wrap"
            android:src="@drawable/line"
            app:layout_constraintBottom_toBottomOf="@id/guideline2"
            app:layout_constraintTop_toTopOf="@id/guideline2"
            app:layout_constraintEnd_toStartOf="@+id/ivotp"
            app:layout_constraintStart_toEndOf="@+id/ivnumberfill"
            />

        <ImageView
            android:id="@+id/ivseconddiv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintWidth_default="wrap"
            android:src="@drawable/line"
            app:layout_constraintBottom_toBottomOf="@id/guideline2"
            app:layout_constraintEnd_toStartOf="@+id/ivprofile"
            app:layout_constraintStart_toEndOf="@id/ivotp"
            app:layout_constraintTop_toTopOf="@id/guideline2" />


        <TextView
            android:id="@+id/tvnumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Mobile Number"
            android:textColor="?colorAccent"
            android:textSize="11sp"
            app:layout_constraintEnd_toEndOf="@id/ivnumberfill"
            app:layout_constraintStart_toStartOf="@id/ivnumberfill"
            app:layout_constraintTop_toBottomOf="@id/ivnumberfill" />

        <TextView
            android:id="@+id/tvotp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="OTP"
            android:textColor="?colorAccent"
            android:textSize="11sp"
            app:layout_constraintEnd_toEndOf="@id/ivotp"
            app:layout_constraintStart_toStartOf="@id/ivotp"
            app:layout_constraintTop_toBottomOf="@id/ivotp" />

        <TextView
            android:id="@+id/tvprofile"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Profile"
            android:textColor="?colorAccent"
            android:textSize="11sp"
            app:layout_constraintEnd_toEndOf="@id/ivprofile"
            app:layout_constraintStart_toStartOf="@id/ivprofile"
            app:layout_constraintTop_toBottomOf="@id/ivprofile" />

        <android.support.constraint.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.2" />

    </android.support.constraint.ConstraintLayout>

this is how it looks

答案 1 :(得分:2)

嗯,您可以使用Constraint布局进行简单的拖放操作,它将为您提供所需的布局。

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="120dp"
    android:background="@color/thirdColor">

    <View
        android:layout_width="272dp"
        android:layout_height="2dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/firstColor"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.333" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.123"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22"
        app:srcCompat="@color/secondColor" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.897"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22"
        app:srcCompat="@color/secondColor" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.533"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.22"
        app:srcCompat="@color/secondColor" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Sample Code"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.535"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.741" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Sample Code"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.971"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.729" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Sample Code"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.063"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.729" />

</android.support.constraint.ConstraintLayout>

示例输出,相应地更改图标和背景。

enter image description here