Android 将视图添加到滚动视图内的布局底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2417221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding view to bottom of layout inside a scrollview
提问by pgsandstrom
So my layout looks basically like this:
所以我的布局基本上是这样的:
<ScrollView>
<RelativeLayout>
<BunchOfViews/>
<ImageView android:layout_alignParentBottom="true"/>
</RelativeLayout>
</ScrollView>
I have the ScrollView
so all of the layout always is visible no matter the height of the screen. The problem is that on a very high screen, I still want my ImageView
to be at the bottom. However, a child of a ScrollView
don't seem to have a defined bottom. The View
is placed at the top of the layout. How can I solve this problem in a neat way?
我有ScrollView
所以无论屏幕高度如何,所有布局始终可见。问题是在非常高的屏幕上,我仍然希望我的屏幕ImageView
位于底部。但是,a 的孩子ScrollView
似乎没有明确的底部。在View
被放置在布局的顶部。我怎样才能以一种巧妙的方式解决这个问题?
回答by dweebo
I ran into the same issue. I never could find a very pleasing solution, but here is how I did it. Maybe someone else has a better way, I hate adding layouts that don't do anything.
我遇到了同样的问题。我从来没有找到一个非常令人满意的解决方案,但我是这样做的。也许其他人有更好的方法,我讨厌添加不做任何事情的布局。
My hack was to add a dummy linearlayout at the bottom of the scrollview that has fill_parent to take up all the room and force the scrollview to fill the screen. Then add whatever component I want to that linearlayout.
我的技巧是在滚动视图底部添加一个虚拟的线性布局,该布局具有 fill_parent 以占据所有空间并强制滚动视图填充屏幕。然后将我想要的任何组件添加到该线性布局中。
Here is one of my layouts that does this:
这是我执行此操作的布局之一:
<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>
回答by Joel Malone
I had the same issue and found this page:
我遇到了同样的问题并找到了这个页面:
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Basically, you set the ScrollView's android:fillViewport
to true, which will allow the child view to expand to the same height as the ScrollView itself, filling out the space. You then just need to have one of the child controls' layout_height
set to fill_parent
and layout_weight
to 1
, causing that control to "spring" to fill the empty space.
基本上,您将 ScrollView 设置android:fillViewport
为 true,这将允许子视图扩展到与 ScrollView 本身相同的高度,填充空间。然后,您只需要拥有子控件的一个layout_height
设置为fill_parent
与layout_weight
对1
,导致该控制‘春天’,以填补空白。
Note that if the contents of the ScrollView are already tall enough to fill the ScrollView, the android:fillViewport
has no effect, so the setting only kicks in when needed.
请注意,如果 ScrollView 的内容已经足够高以填充 ScrollView,则android:fillViewport
该设置无效,因此该设置仅在需要时生效。
My final XML looks like similar to this:
我的最终 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>
回答by Laurent
it seems that the linearlayout isn't necessary, all that is important is the fillViewPort. you could just use
似乎不需要线性布局,重要的是fillViewPort。你可以用
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottomParent="true">
now that you have specified the relativelayout to be at least the size of the screen.
现在您已将相对布局指定为至少是屏幕的大小。
回答by Shohel Rana
its work for me perfectly android:fillViewport="true"
它对我来说非常完美 android:fillViewport="true"
回答by Anthonyeef
Joel Malone's answer Adding view to bottom of layout inside a scrollviewdoes that trick. My solution is almost the same, except that I use Space
widget to do the work of filling the rest height inside the parent layout. Like this:
Joel Malone 的回答将视图添加到滚动视图内的布局底部就是这样做的。我的解决方案几乎相同,只是我使用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>
Attention:
注意力:
fill_parent
in the other answers is noted as deprecated for a long time;- Compare to an empty
LinearLayout
to fill the rest of parent layout, I thinkSpace
is much more appropriate(it's designed to do that job.) - Finally, don't forget that important attribute at the beginning of
ScrollView
:android:fillViewport="true"
. They together make this trick.
fill_parent
在其他答案中被指出已弃用了很长时间;- 与空白
LinearLayout
填充父布局的其余部分相比,我认为Space
更合适(它旨在完成这项工作。) - 最后,不要忘了在开始时是重要的属性
ScrollView
:android:fillViewport="true"
。他们一起做这个把戏。
回答by hitch45
On your view that you want to be at the bottom use android:gravity="bottom"
在你认为你想在底部使用 android:gravity="bottom"
回答by Shibly
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 );
回答by Pentium10
来自:http: //code.google.com/p/k9mail/source/browse/k9mail/trunk/res/layout/account_setup_basics.xml?r=1314
This should help you:
这应该可以帮助您:
<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>