【问题标题】:horizontalscrollview set child elements to fill horizontalscrollview widthHorizo​​ntalscrollview 设置子元素填充 Horizo​​ntalscrollview 宽度
【发布时间】:2011-08-29 13:38:20
【问题描述】:

基本上我有一个水平滚动视图,通过监视我正在分页的 onTouch 事件(即:滚动视图(页面)的每个可见部分在滚动时“点击”到下一个,而不仅仅是有一个标准的 @ 987654321@. 查看 iOS 中的分页滚动视图)。

现在我想找到一种方法让内部子代继承与滚动视图相同的宽度(设置为"fill_parent")。

这是我的 XML 帮助:

<HorizontalScrollView
        android:id="@+id/scrollview"
        android:layout_height="0dp" 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/scrollviewbg">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent">

            <ImageView
                android:src="@drawable/content1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:src="@drawable/content2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:src="@drawable/content3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </HorizontalScrollView>

如您所见,滚动视图设置为宽度fill_parent,其中有一个LinearLayout。现在有三个图像的宽度和高度与屏幕的宽度相似,但我想做的是将其更改为 3 LinearLayouts。

每个LinearLayout 需要继承与滚动视图相同的宽度,以便在应用我的代码时每个都占用一整个“页面”。

我该怎么做?这是我必须使用代码做的事情吗?我需要什么代码?

谢谢。

【问题讨论】:

    标签: android horizontalscrollview


    【解决方案1】:

    我知道它不是直接的答案,但是对于来自 Compatibility Package 的用户 ViewPager 来说,它更容易用于分页功能。您可以在samples 文件夹中找到一个示例(源代码参见src/com/example/android/supportv4/app/FragmentPagerSupport.java)。

    【讨论】:

    • 这要容易得多。感谢那。 :)
    • 我没有使用支持库的选项,我们可以在滚动视图中进行吗
    【解决方案2】:

    试试这个: android:fillViewport="true"

    <HorizontalScrollView
    
        android:fillViewport="true"
    
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:layout_gravity="right"
        android:background="@drawable/black_border"
        android:id="@+id/displayText"
        android:layout_width="match_parent"
        android:scrollHorizontally="true"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:padding="20dp"
        android:text=""
        android:textSize="20sp" />
    </HorizontalScrollView>
    

    在水平滚动视图中,我有一个文本视图,你可以尝试图像视图

    【讨论】:

      【解决方案3】:

      将所有layout_height参数设置为fill_parent。您应该会全屏看到图像。

      【讨论】:

      • 错了!当子视图添加到HorizontalScrollView 时,fill_parent 没有意义。