Horizo​​ntalScrollView中的奇怪边距

时间:2012-12-11 13:23:37

标签: android horizontal-scrolling horizontalscrollview

我需要一个水平滚动的视图列表,所以我将LinearLayout放在Horizo​​ntalScrollView中 并向LinearLayout添加了元素。一切都很好,但有一些奇怪的边缘/填充(我不知道它是什么)

这是完全向左滚动的视图 - 第一项是父母的一半

enter image description here

这是向右滚动的完全滚动 - 右侧的奇怪填充

http://korniltsev.ru/m/gyazo/4d093939766fe.png

这是我的xml:

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

        <LinearLayout
                android:id="@+id/container"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"

                android:layout_gravity="center"/>
</HorizontalScrollView>

项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="150dp"
                android:layout_height="match_parent"
        >

    <FrameLayout
            android:layout_width="140dp"
            android:layout_height="50dp"
            android:background="@drawable/bg_payment_system_item"
            android:layout_centerInParent="true"
            >
        <ImageView
                android:id="@+id/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                />
    </FrameLayout>
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"/>
</RelativeLayout>

2 个答案:

答案 0 :(得分:4)

好的,所以这个行为是一个android bug。 http://code.google.com/p/android/issues/detail?id=20088

答案 1 :(得分:0)

您可以在代码中尝试策略,判断HScroller的内容宽度并与设备/容器宽度进行比较:

  1. 如果内容比容器宽:将内容的重力设置为“左”值

  2. 如果内容比容器更薄:将内容的重力设置为“中心”值

  3. 我认为这只是一个需求的UX设计问题,这个策略对我来说很好。