Android 线性布局 - 如何将元素保持在视图底部?
我有一个 TextView
,我想将其固定在使用 LinearLayout
和垂直排列元素的横向活动的底部。
我已经在文本视图上设置了 android:gravity="bottom"
,但它仍然喜欢位于 LinearLayout
的最后一个元素的正下方,正是我不想要的去做。
有什么建议吗?
I have a TextView
which I want to pin at the bottom of a landscape activity that is using LinearLayout
with vertically arranged elements.
I have set android:gravity="bottom"
on the text view, but it still likes to be just below the last element of the LinearLayout
exactly what I do not want it to do.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您必须通过设置
android:layout_weight="1"
来扩展上面的视图之一以填充剩余空间。这会将您的最后一个视图推到底部。以下是我的意思的简要概述:
其中每个子视图高度都是
“wrap_content”
,其他所有内容都是“fill_parent”
。You will have to expand one of your upper views to fill the remaining space by setting
android:layout_weight="1"
on it. This will push your last view down to the bottom.Here is a brief sketch of what I mean:
where each of the child view heights is
"wrap_content"
and everything else is"fill_parent"
.更新:我仍然在这个问题上得到了赞成,这仍然是公认的答案,我认为我回答得不好。本着确保提供最佳信息的精神,我决定更新此答案。
在现代 Android 中,我将使用
ConstraintLayout
来执行此操作。它的性能更高,也更简单。如果您不想使用 ConstraintLayout,使用具有扩展视图的 LinearLayout 是处理占用额外空间的简单且很好的方法(请参阅答案@马修·威尔斯)。如果你不想扩展底视图上方任何View的背景,你可以添加一个不可见的View来占用空间。
我最初给出的答案有效,但效率低下。对于单个顶级布局来说,效率低下可能不是什么大问题,但在
ListView
或RecyclerView
中实现会很糟糕,而且没有任何理由这样做这样做是因为有更好的方法可以做到这一点,即使不是更简单,也大致相同级别的工作量和复杂性。将 TextView 从 LinearLayout 中取出,然后将 LinearLayout 和 TextView 放入relativelayout 中。将属性
android:layout_alignParentBottom="true"
添加到 TextView。除上述属性外,所有命名空间和其他属性均已删除:Update: I still get upvotes on this question, which is still the accepted answer and which I think I answered poorly. In the spirit of making sure the best info is out there, I have decided to update this answer.
In modern Android I would use
ConstraintLayout
to do this. It is more performant and straightforward.If you don't want to use a ConstraintLayout, using a LinearLayout with an expanding view is a straightforward and great way to handle taking up the extra space (see the answer by @Matthew Wills). If you don't want to expand the background of any of the Views above the bottom view, you can add an invisible View to take up the space.
The answer I originally gave works but is inefficient. Inefficiency may not be a big deal for a single top level layout, but it would be a terrible implementation in a
ListView
orRecyclerView
, and there just isn't any reason to do it since there are better ways to do it that are roughly the same level of effort and complexity if not simpler.Take the TextView out of the LinearLayout, then put the LinearLayout and the TextView inside a RelativeLayout. Add the attribute
android:layout_alignParentBottom="true"
to the TextView. With all the namespace and other attributes except for the above attribute elided:我认为这将是完美的解决方案:
I think it will be perfect solution:
步骤 1:在线性布局中创建两个视图
步骤 2:第一个视图必须设置为 android:layout_weight="1"
步骤 3:第二个视图将自动向下放置
Step 1 : Create two view inside a linear layout
Step 2 : First view must set to android:layout_weight="1"
Step 3 : Second view will automatically putted downwards
您应该将参数重力到底部不要放在文本视图中,而是放在线性布局中。像这样:
You should put the parameter gravity to bottom not in the textview but in the Linear Layout. Like this:
您也可以
用于您的文本视图
You can also use
for your textview
喜欢这个
DO LIKE THIS
只需在 LinearLayout 中使用
android:gravity="bottom"
simply use
android:gravity="bottom"
in LinearLayout试试这个
try this