android - ScrollView 及其内容使用“wrap_content”。如何分配约束优先级?
问题描述
我需要一个ScrollView
具有固定高度100dp
和动态内容的 ImageViews 水平。
目前我的 ScrollView 有一个水平LinearLayout
的ImageView
s。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>
解决方案
好的,这比我想象的要容易修复:
ScrollView
s 总是垂直的。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">
推荐阅读
- python - 如何在不同的文件中运行多个 Keras 神经网络模型?
- c++ - 动态构造函数中的 new char [] 抛出“损坏的顶部大小”;
- python - as_string() 方法返回一个 AttributeError
- c# - 单元格更改时Datagrid将值保存在数据库中
- sql - 如何从常见的列值中创建一个数组?
- excel - 如何从动态表中根据变量选择范围?
- javascript - 如果模糊事件的验证失败,则阻止操作
- sorting - 在 jQuery Mobile 中单击按钮时对列表视图进行排序
- java - 如何在数组中显示大于/小于用户数的随机值?- 爪哇
- java - 当前令牌的生命周期