android - HorizontalScrollView 中的 Android 中心子项
问题描述
我在 ImageView 顶部有一个带有 ImageView 和 RelativeLayout 的 HorizontalScrollView。RelativeLayout 获取在运行时动态创建和分配的多个 ImageButton。
<HorizontalScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<ImageView
android:id="@+id/scene"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:contentDescription="@string/indoor_tour_scene_view"
android:adjustViewBounds="true"
>
</ImageView>
<RelativeLayout
android:id="@+id/coordinate_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignTop="@id/scene"
>
<!-- Mapstops will be added programmatically -->
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
在纵向模式下,ImageView 填充视口,HorizontalScrollView 的高度和宽度重叠,因此您必须左右滚动才能看到所有内容。
在横向模式下,高度仍然是 100%,但由于图像比例,宽度会小于视口。所以我想在 HorizontalScrollView 中居中视图。
layout_gravity="center"
当我在 RelativeLayout 上使用时,我遇到了一些偏移问题。我发现这个博客条目https://content.pivotal.io/blog/centering-a-view-within-a-scrollview说它是一个 android 错误,我添加了那里描述的解决方法。它根据偏移量起作用,但现在我的 RelativeLayout@+id/coordinate_container
填充了整个框架。这太大了,实际上应该匹配 ImageView 的大小,因为动态创建的 ImageButton 位置是相对于 ImageView 的。
我怎样才能让 HorizontalScrollView 的孩子在没有偏移错误的情况下居中,但也有相对于 ImageView 内容的坐标容器,所以它不会填满整个框架?
解决方案
推荐阅读
- javascript - 在 React 中,在下拉列表中进行提取时我应该依赖哪个值?e.target 值或来自状态的值
- .net - 在 .NET 中确定网络路径的文件系统
- python-3.x - `Drop column by id` 的方式会导致在数据框中删除所有相同的列
- python - 根据条件随机选择数组中的位置
- sql-server - 除了维护 FIFO 属性之外,SQL 还可以捕获运行总计
- angular - 出现错误无法匹配任何路由。网址段:“家”
- sms - 以编程方式发送/接收短信
- bash - 检查文件是否存在在启动守护进程中不起作用
- laravel - 通过 AWS SES 使用 Laravel 发送批量邮件的正确方法是什么?
- processing - 为什么我的打印功能打印出乱码信息?