參考自這位哥,歡迎到這里看: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>