【问题标题】:How do I maintain aspect ratio of an imageview like in this application? [Android, ImageView, Aspect ratio, ScrollView]如何在此应用程序中保持图像视图的纵横比? 【Android、ImageView、长宽比、ScrollView】
【发布时间】:2019-12-22 00:27:15
【问题描述】:

我想实现图像视图和按钮组的纵横比,如下图 1 所示。图 2 是我活动的当前状态。我想达到与图1中的应用程序相同的状态。

编辑 1 - 图片一上的应用程序始终是 imageview + 按钮组,一个所有设备,所以这不是纵横比的问题,而是别的东西

图像视图和按钮组被包裹在一个约束布局中,它位于滚动视图内部。

图片1 - 图二——

下面的代码是关于 ImageView 和按钮组的部分。

<ImageView
        android:id="@+id/poster_image"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@id/buttons_group"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/buttons_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/poster_image"
        app:layout_constraintBottom_toTopOf="@id/imdb_placeholder"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="parent">

    <Button
            android:id="@+id/trailer_button"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@drawable/ic_play"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/watch_later_button"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"/>

    <Button
            android:id="@+id/watch_later_button"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@drawable/ic_watch_later"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"
            app:layout_constraintStart_toEndOf="@+id/trailer_button"
            app:layout_constraintEnd_toStartOf="@+id/share_movie_button"/>
    <Button
            android:id="@+id/share_movie_button"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:background="@drawable/ic_share"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"
            app:layout_constraintStart_toEndOf="@+id/watch_later_button"
            app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 视图需要滚动吗?您是否要让图片占据整个屏幕并让按钮在顶部滚动?
  • 是的,有点。图片和按钮应该占据屏幕高度,但它们在滚动视图内,所以我想向下滚动。有什么想法吗?
  • 您对 RecyclerViews 感觉如何?
  • 我们又爱又恨哈哈,为什么呢?我指的应用是TodoMovieList,
  • 我知道使用 Recycler 视图完成此任务的一种相当简单的方法。

标签: java android xml kotlin imageview


【解决方案1】:

使用RecyclerView,您可以提供多种视图类型。

enum class ViewType {
    HEADER_CONTROLS,
    ADDITIONAL_CONTROLS,
    COMMENTS
}

class Adapter() {
    fun getViewType(position: Int): Int {
        when (position) {
            0 -> ViewType.HEADER_CONTROLS.ordinal
            // ETC
        }
    }
}

然后您可以将标题视图创建为匹配父级,它会适当地滚动。

【讨论】:

  • 感谢您的提示,试了一下。仍然没有得到图片 1 上的内容,相同的比例在不同设备上显示不同,而图片 1 中的应用在所有设备上保持相同。滚动视图的顶部始终是 imageview+button group
  • 抱歉回复晚了。我应该将我想要在标题视图中的内容封装为 recyclerview 元素吗?我从未使用过具有多种视图类型的 recyclerview
  • 您可以将其分解为每个视图或分组,以使您感觉更舒服。我都做过。两者都行得通。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多