android - 如何在 View 上应用 Material 设计规范中的纵横比?

标签 android android-layout material-design android-cardview aspect-ratio

我正在为我的应用程序设计一个带有富媒体 header 的 CardView

我试着做这样的东西:

Rich media header from google material design specification

根据谷歌 Material 设计规范,图片应具有 16:9 的纵横比:

Google rich media header specification

那么,我的问题是,如何实现这个(代码或 XML)?

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

否则,我没有成功通过代码设置大小,因为在 onBindViewHolder(...) 中,getWidth() 在我的 View 中返回 0。

有什么想法吗?

最佳答案

现在 PercentFrameLayoutPercentRelativeLayout 在 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/

相关文章:

java - 以编程方式向布局添加多个按钮

Android - ScrollView 和 PercentRelativeLayout

android - 如果 Spinner 或 EditText 更改,则更改 TextView

Android - VideoView 需要按 BACK 两次才能退出

android - 在 kotlin 中,如何创建个人资料图像并在离线短信应用程序中将联系人姓名的第一个字母显示为个人资料图像?

java - 使用 DrawerLayout 在 fragment 中设置工具栏标题

java - 如何单击歌曲列表(正在播放 Activity )在没有(音乐预览)startActivity 的情况下播放

android - 如何在Android中加载不同国家的特定文件

android - 以编程方式创建具有轮廓样式的 MaterialButton

ios - Material Vertical 步进形式可以在 Swift iOS Xcode 中实现