我正在尝试弄清楚如何使用约束布局实现以下行为:
- 在 ConstraintLayout 父级的中心放置一个 View
- 使 View 宽度为父 View 宽度的一半
- 使 View 的高度等于它的宽度
(即 - 在中心放置一个正方形)
我尝试使用这个组合:
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintDimensionRatio="2:1"
但我不确定如何从这里继续
最佳答案
您无需指南即可轻松完成。
只需使用app:layout_constraintWidth_percent="0.5"
它适用于 ConstraintLayout:1.1.0-beta5 及更高版本。
<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">
<ImageView
android:id="@+id/img_icon"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/dark_red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5" />
</android.support.constraint.ConstraintLayout>
关于android - ConstraintLayout - 与自身成比例的宽度/高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49639231/