android-两个控件中心线对齐

参考自这位哥,欢迎到这里看:https://www.niwoxuexi.com/blog/android00/article/274.html

假如控件A的大小是wrap-content,控件B的大小也是wrap-content,而且控件A和控件B,在水平方向上 中心点对齐,要怎么做呢?

这个问题,在iOS里面有ConstraintCenter可以实现。android里面有五大布局。该选择哪个实现?百度搜了好几次,都没有讲清楚,说的都是相对于父控件中心点对齐。

实践了一下,除了ConstraintLayout,好像RelativeLayout是实现不了的。

正确的姿势:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            style="@style/text_project_item"
            android:textStyle="bold"
            android:text="@string/structure_statistic_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dp_15"
            android:layout_marginBottom="@dimen/dp_15"
            android:layout_marginTop="15dp"
            />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:background="@color/gray"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="@dimen/dp_15"
            android:layout_centerInParent="true"
            tools:ignore="ExtraText">
            <TextView
                android:id="@+id/structure_delete_tv"
                style="@style/text_project_item"
                android:textStyle="bold"
                android:textColor="@color/CAAAAAA"
                android:textSize="30sp"
                android:text="删除"
                android:background="@color/gray"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                app:layout_constraintTop_toTopOf="@+id/ic_structure_delte"
                app:layout_constraintBottom_toBottomOf="@+id/ic_structure_delte"
                app:layout_constraintRight_toLeftOf="@+id/ic_structure_delte" />

            <ImageView
                android:id="@+id/ic_structure_delte"
                android:background="@color/color2"
                app:srcCompat="@drawable/menu_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </RelativeLayout>

 

原文地址:https://www.cnblogs.com/qi-dev/p/13807024.html