首页 > 解决方案 > ScrollView 及其内容使用“wrap_content”。如何分配约束优先级?

问题描述

我需要一个ScrollView具有固定高度100dp和动态内容的 ImageViews 水平。

目前我的 ScrollView 有一个水平LinearLayoutImageViews。ImageViews 已经wrap_content启用了它们的宽度,而所有它们的高度都是固定的。ScrollView 本身的高度为wrap_content.

如果 ScrollViews 的内容大于视图可以在不滚动的情况下显示的内容,则 ImageViews 图像将按比例缩小。ImageViewslayout_hight根据其边界工作,但图像本身不是。

换成scaleType别的没用。为 LinearLayout 设置固定宽度是可行的,但由于内容应该是动态的,所以这不是一个选项。这似乎是一个默认用例。这不可能xml吗?

手动给定精确 ScrollView-width 的示例

该视图看起来像我需要的那样,但不允许动态内容。

在此处输入图像描述

宽度为 on 的示例wrap_content

图像视图

在此处输入图像描述

滚动视图代码如下:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#CCCCFF">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#AAAAFF"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/so1"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/so2"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

        <Space
            android:layout_width="10dp"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/so3"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:adjustViewBounds="true"
            android:background="#FF0000"
            android:scaleType="fitCenter"
            android:src="@drawable/so" />

    </LinearLayout>
</ScrollView>

标签: androidandroid-layoutandroid-scrollviewandroid-wrap-content

解决方案


好的,这比我想象的要容易修复:

ScrollViews 总是垂直的。ScrollView 中没有方向属性。要获得水平 ScrollView,请改用HorizontalScrollView并放弃方向。

所以这

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

应该改为:

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

推荐阅读