如何在视图中应用材料设计规格的宽高比?

Pie*_*rre 4 android aspect-ratio android-layout material-design android-cardview

我正在CardView为我的应用程序设计一个富媒体标题.

我尝试做这样的事情:

来自谷歌材料设计规范的富媒体标题

根据谷歌材料设计规范,图片应该有16:9的宽高比:

Google富媒体标头规范

所以,我的问题,如何实现这个(代码或XML)?

如果我使用定义的大小,它将不是真正的16:9宽高比,我将不得不处理所有屏幕尺寸和方向的许多资源文件.

否则,我没有成功的代码,因为在设置大小onBindViewHolder(...),getWidth()对我的看法回报0.

任何的想法 ?

Eug*_*sov 8

现在使用PercentFrameLayoutPercentRelativeLayout在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>
Run Code Online (Sandbox Code Playgroud)

您可以在此处通过ConstraintLayout查看其他卡实现.所有这些都是按照材料设计指南制造的.