代码之家  ›  专栏  ›  技术社区  ›  pgsandstrom

在滚动视图中的布局底部添加视图

  •  59
  • pgsandstrom  · 技术社区  · 15 年前

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

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

    我有 ScrollView 因此,无论屏幕的高度如何,所有布局都始终可见。问题是在一个很高的屏幕上,我仍然想要 ImageView 在底部。但是,一个 卷轴视图 似乎没有明确的底线。这个 View 放置在布局的顶部。我怎样才能巧妙地解决这个问题?

    8 回复  |  直到 7 年前
        1
  •  135
  •   VCD    12 年前

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

    我的诀窍是在scrollview的底部添加一个虚拟的linearlayout,让fill_parent占据所有的空间,并强制scrollview填充屏幕。然后把我想要的组件添加到线性布局中。

    下面是我的一个布局:

    <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>
    
        2
  •  65
  •   Joel Malone    14 年前

    我也有同样的问题,发现这一页:

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

    基本上,你设置了 android:fillViewport 为true,这将允许子视图扩展到与ScrollView本身相同的高度,从而填充空间。你只需要有一个子控件 layout_height 设置为 fill_parent layout_weight 1 ,使该控件“spring”填满空白空间。

    注意,如果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>
    
        3
  •  20
  •   Laurent    15 年前

    似乎不需要线性布局,重要的是fillviewport。 你可以用

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

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

        4
  •  13
  •   Shohel Rana    8 年前

    它对我来说是完美的android:fillviewport=“true”

        5
  •  6
  •   Community CDub    8 年前

    乔尔·马龙的回答 Adding view to bottom of layout inside a scrollview 玩那个把戏。我的解决方案几乎相同,除了我使用 Space 小部件来完成在父布局中填充剩余高度的工作。这样地:

    <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>
    

    注意:

    • fill_parent 在其他答案中被认为是长期不赞成的;
    • 与空的比较 LinearLayout 我认为,要填充父布局的其余部分 空间 更合适(它是为完成这项工作而设计的)。
    • 最后,不要忘记 ScrollView : android:fillViewport="true" . 他们一起做这个把戏。
        6
  •  1
  •   hitch45    11 年前

    如果您认为自己想处于最底层,请使用android:gravity=“bottom”

        7
  •  0
  •   Pentium10    15 年前

    来自: http://code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314

    这将有助于您:

    <RelativeLayout
            android:layout_marginTop="-45dip" 
            android:padding="0dip"
            android:layout_alignParentBottom="true"
            android:gravity="bottom|right" 
            android:background="@android:drawable/bottom_bar"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
            <Button
                android:id="@+id/manual_setup"
                android:text="@string/account_setup_basics_manual_setup_action"
                android:minWidth="@dimen/button_minWidth"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginBottom="-4dip" 
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="false" 
                />
            <Button
                android:id="@+id/next"
                android:text="@string/next_action"
                android:minWidth="@dimen/button_minWidth"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:drawableRight="@drawable/button_indicator_next"
                android:layout_marginBottom="-4dip" 
                android:layout_alignParentRight="true"
                android:layout_centerVertical="false" 
                />
        </RelativeLayout>
    
        8
  •  0
  •   Shibly    13 年前
    ScrollView view = new ScrollView( this );
    ScrollView.LayoutParams lps = new FrameLayout.LayoutParams( FILL_PARENT, FILL_PARENT, Gravity.CENTER );
    LinearLayout layout = new LinearLayout( this );
    // what ever you want in the Layout 
    view.addView( layout, lps );