在scrollview中向布局底部添加视图

pgs*_*rom 65 layout android scroll

所以我的布局看起来基本上像这样:

<ScrollView>
    <RelativeLayout>
        <BunchOfViews/>
        <ImageView android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

我有,ScrollView所以无论屏幕高度如何,所有布局始终都是可见的.问题是在非常高的屏幕上,我仍然希望我ImageView处于最底层.然而,一个孩子ScrollView似乎没有明确的底部.在View被放置在布局的顶部.如何以一种简洁的方式解决这个问题?

dwe*_*ebo 147

我遇到了同样的问题.我从来没有找到一个非常令人愉快的解决方案,但这就是我如何做到的.也许别人有更好的方法,我讨厌添加不做任何事情的布局.

我的hack是在scrollview底部添加一个虚拟线性布局,其中fill_parent占用了所有空间并强制滚动视图填满屏幕.然后将我想要的任何组件添加到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>
Run Code Online (Sandbox Code Playgroud)

  • android:fillViewport是这个技巧的重要组成部分,很容易错过. (52认同)
  • 'android:layout_below ="@ + id/spinner"'部分可能不应该在'id'之前指定'+'...可能这不是第一次定义id的地方(它会是在"这里的一堆组件"部分的某处定义,并且它将具有+ id) (2认同)

Joe*_*one 72

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

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

基本上,你设置了滚动的android:fillViewport为真,这将让孩子视图扩展到相同的高度滚动型本身,填写的空间.然后,您只需将其中一个子控件layout_height设置为fill_parent和,layout_weight1使该控件"弹出"以填充空白区域.

请注意,如果滚动型的内容已经足够高,以填补了滚动,则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>
Run Code Online (Sandbox Code Playgroud)


小智 20

似乎线性布局不是必需的,重要的是fillViewPort.你可以使用

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottomParent="true">
Run Code Online (Sandbox Code Playgroud)

现在你已经指定relativelayout至少是屏幕的大小.

  • 这实际上是有效的.答案还不够清楚 - 将`fillViewPort ="true"添加到你的`ScrollView`中. (6认同)

Sho*_*ana 13

它为我的工作完美android:fillViewport ="true"


Ant*_*eef 8

Joel Malone的回答在scrollview添加视图到底部布局就可以了.我的解决方案几乎相同,只是我使用Spacewidget来完成填充父布局中的其余高度的工作.像这样:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent
    android:fillViewport="true"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        >
        <android.support.v4.widget.Space
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
    <ImageView
        android:layout_height="wrap_content"
        android:src="@drawable/footer_image"
        />
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

注意:

  • fill_parent 在其他答案中被注意到长期被弃用;
  • 比较一个空LinearLayout来填充其余的父布局,我认为Space更合适(它的目的是做那个工作.)
  • 最后,不要忘记开头的重要属性ScrollView:android:fillViewport="true".他们一起做这个伎俩.