首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将视图添加到滚动视图内布局的底部

将视图添加到滚动视图内布局的底部
EN

Stack Overflow用户
提问于 2010-03-10 21:36:10
回答 8查看 62.3K关注 0票数 71

所以我的布局基本上是这样的:

<ScrollView>
    <RelativeLayout>
        <BunchOfViews/>
        <ImageView android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</ScrollView>

我有ScrollView,所以无论屏幕的高度如何,所有的布局都是可见的。问题是,在一个非常高的屏幕上,我仍然希望我的ImageView在底部。然而,ScrollView的子类似乎没有定义好的底部。View被放置在布局的顶部。我怎样才能以一种整洁的方式解决这个问题?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-03-10 23:13:49

我遇到了同样的问题。我从来没有找到一个非常令人满意的解决方案,但以下是我是如何做到的。也许其他人有更好的方法,我讨厌添加不做任何事情的布局。

我的诀窍是在scrollview的底部添加一个虚拟的linearlayout,让fill_parent占据所有的空间,并迫使scrollview充满屏幕。然后将我想要的任何组件添加到该linearlayout中。

这是我的一个布局,可以做到这一点:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="15px" >

        <!-- bunch of components here -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/spinner"
            android:layout_marginTop="5px"
            android:gravity="center_horizontal|bottom"
            android:paddingTop="2px" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="20px"
                android:paddingRight="20px"
                android:text="Delete" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>
票数 167
EN

Stack Overflow用户

发布于 2010-11-24 00:46:04

我遇到了同样的问题,并找到了这个页面:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

基本上,您将ScrollView的android:fillViewport设置为true,这将允许子视图扩展到与ScrollView本身相同的高度,从而填充空间。然后,您只需要将其中一个子控件的layout_height设置为fill_parent,并将layout_weight设置为1,从而使该控件“弹簧”来填充空白空间。

请注意,如果ScrollView的内容已经足够填充ScrollView,则android:fillViewport不起作用,因此该设置仅在需要时生效。

我的最终XML如下所示:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <!-- this expands to fill the empty space if needed -->
        </LinearLayout>

        <!-- this sits at the bottom of the ScrollView,
        getting pushed out of view if the ScrollView's
        content is tall enough -->
        <ImageView
            android:layout_height="wrap_content"
            android:src="@drawable/footer_image">
        </ImageView>
    </LinearLayout>
</ScrollView>
票数 79
EN

Stack Overflow用户

发布于 2010-06-17 19:52:08

看起来linearlayout不是必须的,重要的是fillViewPort。你可以直接使用

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottomParent="true">

现在您已经将relativelayout指定为至少是屏幕的大小。

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2417221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档