我正在为我的应用程序设计一个带有富媒体 header 的 CardView
。
我试着做这样的东西:
根据谷歌 Material 设计规范,图片应具有 16:9 的纵横比:
那么,我的问题是,如何实现这个(代码或 XML)?
如果我使用定义的尺寸,它不会是真正的 16:9 宽高比,我将不得不处理所有屏幕尺寸和方向的许多资源文件。
否则,我没有成功通过代码设置大小,因为在 onBindViewHolder(...)
中,getWidth()
在我的 View 中返回 0。
有什么想法吗?
最佳答案
现在 PercentFrameLayout
和 PercentRelativeLayout
在 26.0.0 中被弃用,您应该考虑使用 ConstraintLayout构建您的卡片。
这是您案例的 xml fragment :
<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 .所有这些都是按照 Material design guidelines build 的.
关于android - 如何在 View 上应用 Material 设计规范中的纵横比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374941/