【问题标题】:Strange margins in HorizontalScrollViewHorizo​​ntalScrollView 中的奇怪边距
【发布时间】:2012-11-29 01:44:26
【问题描述】:

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

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

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

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>

【问题讨论】:

  • 似乎 Horizo​​ntalScrollview 或其父级定义了某种填充/边距。
  • OMFG!刚才提到右边的padding随时间变化!!
  • 尝试将Horizo​​ntalScrollView中的重力设置为中心?
  • @aswin-kumar HSView 中没有 souch 属性

标签: android horizontal-scrolling horizontalscrollview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】: