Android:ConstraintLayout尺寸比例+宽度百分比
Android: ConstraintLayout dimension ratio + width percent
我希望下面布局中的图像能够随着不同的屏幕尺寸进行缩放。它必须保持 1:1 的纵横比,但不能大于 150dp
我尝试使用 Width_max
、DimensionRatio
和 Width_percent
的组合,但图像要么遵循宽度百分比,要么缩小到两个相邻文本视图的组合高度
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_margin="4dp"
android:background="@android:color/white"
app:cardCornerRadius="4dp"
app:cardElevation="@dimen/cardElevation"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnailImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
android:scaleType="centerCrop"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/titleTextView"
app:layout_constraintWidth_max="150dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintWidth_percent="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/placeholder"
android:textColor="@android:color/black"
android:textSize="@dimen/textMedium"
app:layout_constraintBottom_toTopOf="@id/descriptionTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/thumbnailImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="@string/placeholder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/titleTextView"
app:layout_constraintStart_toStartOf="@id/titleTextView"
app:layout_constraintTop_toBottomOf="@id/titleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
这是 ConstraintLayout
的奇怪行为
但我找到了一种替代方法,可以使用 Guideline
使尺寸正确跟随
在 ConstraintLayout
中以 20% 的宽度添加 Guideline
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".2" />
使 ImageView
的 layout_constraintEnd_toStartOf
指向准则
<ImageView
...
app:layout_constraintEnd_toStartOf="@id/guideline2"
... />
从 ImageView
中删除 app:layout_constraintWidth_percent="0.2"
Leaving the layout_constraintWidth_max
of the ImageView
at 150dp
will constraint it to a maximum of 150dp
but if the guideline comes before 150dp
it follow the guideline as a constraint.
我希望下面布局中的图像能够随着不同的屏幕尺寸进行缩放。它必须保持 1:1 的纵横比,但不能大于 150dp
我尝试使用 Width_max
、DimensionRatio
和 Width_percent
的组合,但图像要么遵循宽度百分比,要么缩小到两个相邻文本视图的组合高度
<androidx.cardview.widget.CardView 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="wrap_content"
android:layout_margin="4dp"
android:background="@android:color/white"
app:cardCornerRadius="4dp"
app:cardElevation="@dimen/cardElevation"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnailImageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
android:scaleType="centerCrop"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/titleTextView"
app:layout_constraintWidth_max="150dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintWidth_percent="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/placeholder"
android:textColor="@android:color/black"
android:textSize="@dimen/textMedium"
app:layout_constraintBottom_toTopOf="@id/descriptionTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/thumbnailImageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="@string/placeholder"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/titleTextView"
app:layout_constraintStart_toStartOf="@id/titleTextView"
app:layout_constraintTop_toBottomOf="@id/titleTextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
这是 ConstraintLayout
但我找到了一种替代方法,可以使用 Guideline
在 ConstraintLayout
Guideline
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".2" />
使 ImageView
的 layout_constraintEnd_toStartOf
指向准则
<ImageView
...
app:layout_constraintEnd_toStartOf="@id/guideline2"
... />
从 ImageView
中删除app:layout_constraintWidth_percent="0.2"
Leaving the
layout_constraintWidth_max
of theImageView
at150dp
will constraint it to a maximum of150dp
but if the guideline comes before150dp
it follow the guideline as a constraint.