横向滚动视图设置子元素以填充横向滚动视图宽度

3

我有一个水平滚动视图,通过监控onTouch事件来分页(即:每个可见的滚动视图部分(页面)在滚动时“点击”到下一个,而不是只有标准的ScrollView。请参见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个LinearLayout进行更改。
每个LinearLayout需要继承与滚动视图相同的宽度,以便在应用我的代码时,每个都占据整个“页面”。
我该如何做?这是我必须使用代码完成的事情吗?我需要哪些代码?
谢谢。
3个回答

3

虽然不是直接的回答,但使用兼容性包中的ViewPager实现分页功能会更加容易。您可以在samples文件夹中找到一个例子(请查看源代码src/com/example/android/supportv4/app/FragmentPagerSupport.java)。


那样做会容易得多。谢谢。 :) - Thomas Clayson
我没有使用支持库的选项,我们可以在scrollview中实现它吗? - user606669

2

Try this: 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>

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

0
将所有的layout_height参数设置为fill_parent。然后你应该能够看到全屏的图片。

1
当子视图添加到HorizontalScrollView时,fill_parent是不合适的。 - neevek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接