首页 > 解决方案 > Horizo​​ntalScrollView 中的 Android 中心子项

问题描述

我在 ImageView 顶部有一个带有 ImageView 和 RelativeLayout 的 Horizo​​ntalScrollView。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 填充视口,Horizo​​ntalScrollView 的高度和宽度重叠,因此您必须左右滚动才能看到所有内容。

在横向模式下,高度仍然是 100%,但由于图像比例,宽度会小于视口。所以我想在 Horizo​​ntalScrollView 中居中视图。

在此处输入图像描述

layout_gravity="center"当我在 RelativeLayout 上使用时,我遇到了一些偏移问题。我发现这个博客条目https://content.pivotal.io/blog/centering-a-view-within-a-scrollview说它是一个 android 错误,我添加了那里描述的解决方法。它根据偏移量起作用,但现在我的 RelativeLayout@+id/coordinate_container填充了整个框架。这太大了,实际上应该匹配 ImageView 的大小,因为动态创建的 ImageButton 位置是相对于 ImageView 的。

我怎样才能让 Horizo​​ntalScrollView 的孩子在没有偏移错误的情况下居中,但也有相对于 ImageView 内容的坐标容器,所以它不会填满整个框架?

标签: androidcenterhorizontalscrollview

解决方案


推荐阅读