如何在视图上应用 material 设计规范中的纵横比?
How to apply aspect ratio from material design specs on a view?
我正在为我的带有富媒体 header 的应用程序设计 CardView
。
我试着做这样的东西:
根据googlematerial设计规范,图片应16:9宽高比:
所以,我的问题是,如何实现这个(代码或XML)?
如果我使用定义的尺寸,它将不是真正的 16:9 宽高比,我将不得不处理所有屏幕尺寸和方向的许多资源文件。
否则,我没有成功通过代码设置大小,因为在 onBindViewHolder(...)
、getWidth()
我认为 return 0.
有什么想法吗?
最简单的方法是使用 PercentRelativeLayout
作为父级 ViewGroup
。然后,您可以在 ImageView
上设置以下 XML 属性
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
另一种选择是编写您自己的自定义 ImageView
并覆盖 onMeasure()
以确保视图以 16:9 比率结束。 This 答案告诉你如何去做。
现在 PercentFrameLayout
和 PercentRelativeLayout
在 26.0.0 中被弃用,您应该考虑使用 ConstraintLayout 来构建您的卡片。
这是您案例的 xml 片段:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/media_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginBottom="16dp"
android:scaleType="centerCrop"
app:srcCompat="@android:color/darker_gray"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/supporting_text"
app:layout_constraintVertical_chainStyle="spread_inside" />
<TextView
android:id="@+id/supporting_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Greyhound divisively hello coldly wonderfully marginally far upon excluding."
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#DE000000"
app:layout_constraintTop_toBottomOf="@+id/media_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
您可以通过 ConstraintLayout 查看其他卡片实现 here. All of them were built in compliance with Material design guidelines。
我正在为我的带有富媒体 header 的应用程序设计 CardView
。
我试着做这样的东西:
根据googlematerial设计规范,图片应16:9宽高比:
所以,我的问题是,如何实现这个(代码或XML)?
如果我使用定义的尺寸,它将不是真正的 16:9 宽高比,我将不得不处理所有屏幕尺寸和方向的许多资源文件。
否则,我没有成功通过代码设置大小,因为在 onBindViewHolder(...)
、getWidth()
我认为 return 0.
有什么想法吗?
最简单的方法是使用 PercentRelativeLayout
作为父级 ViewGroup
。然后,您可以在 ImageView
app:layout_widthPercent="100%"
app:layout_aspectRatio="178%"
另一种选择是编写您自己的自定义 ImageView
并覆盖 onMeasure()
以确保视图以 16:9 比率结束。 This 答案告诉你如何去做。
现在 PercentFrameLayout
和 PercentRelativeLayout
在 26.0.0 中被弃用,您应该考虑使用 ConstraintLayout 来构建您的卡片。
这是您案例的 xml 片段:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/media_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:layout_marginBottom="16dp"
android:scaleType="centerCrop"
app:srcCompat="@android:color/darker_gray"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/supporting_text"
app:layout_constraintVertical_chainStyle="spread_inside" />
<TextView
android:id="@+id/supporting_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Greyhound divisively hello coldly wonderfully marginally far upon excluding."
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#DE000000"
app:layout_constraintTop_toBottomOf="@+id/media_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
您可以通过 ConstraintLayout 查看其他卡片实现 here. All of them were built in compliance with Material design guidelines。