布局帮助,消失的 LinearLayouts
我正在尝试设计一个简单的布局,其中有两个按钮,位于页面底部中心的水平线性布局中(因此layout_gravity =“bottom”)。我还需要一个位于屏幕中心的垂直线性布局,可以容纳 3 个 TextView(稍后在程序中实现,因此只需要一个 LinearLayout 能够容纳 3 个 TextView)。
我可以将按钮放在正确的位置(尽管我希望它们更加分开),但每次我尝试使 TextViews LinearLayout 时,它要么不出现,要么使另一个消失。我一直在努力解决这个问题有一段时间了...有人可以帮忙吗?谢谢
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/background">
<LinearLayout
android:id="@+id/game_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_margin="100dip">
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_label"
android:layout_gravity="center"/>
<Button
android:id="@+id/repeat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/repeat_label"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
I'm trying to design a simple layout with 2 buttons in a horizontal linear layout centered at the bottom of the page (so layout_gravity="bottom"). And I also need a vertical linear layout that's centered in the screen that can hold 3 TextViews (that are implemented later in the program, so just need a LinearLayout to be ABLE to hold 3 TextViews).
I can get the buttons to their right spots (even though I would like them more seperated), but every time I try to make the TextViews LinearLayout it either doesn't appear or makes the other one disappear. I've been trying to get this down for awhile...can anybody help? Thanks
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/background">
<LinearLayout
android:id="@+id/game_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:layout_margin="100dip">
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_label"
android:layout_gravity="center"/>
<Button
android:id="@+id/repeat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/repeat_label"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Linearlayout2 是垂直布局,子级位于中心,
这是您需要的吗?
EditText
以及“Some text”值通过 java 添加>LayoutParams 设置为WRAP_CONTENT
linearlayout2
is vertical layout with children positioned in the centerIs that what you need?
EditText
with "Some text" value is added through java with bothLayoutParams
set toWRAP_CONTENT
确保 2 个
Button
的水平LinearLayout
的高度设置为wrap_content
。您也可以采用
RelativeLayout
并保留两个分别垂直和水平方向的LinearLayout
。此代码就足够了:
警告:属性的大小写可能不同,因此请更正它们。
Make sure the horizontal
LinearLayout
of 2Button
s has its height set towrap_content
.Also you can take a
RelativeLayout
and keep twoLinearLayout
of vertical and horizontal orientation respectively.This code shall suffice:
Caveat: casing of properties could be different so correct them.
快速想法:
将垂直
LinearLayout
的高度设置为 0dp,并使用weight
为其提供其他视图布局后剩余空间的其余部分,例如android:layout_weight="1"
棘手的部分是需要将高度设置为 0,以便它实际上使用
layout_weight
。警告:已经很晚了,我已经喝了几杯酒了。 :)
Quick idea:
Set the vertical
LinearLayout
's height to 0dp, and useweight
to give it the rest of all the remaining space after your other view is laid out, e.g. give itandroid:layout_weight="1"
The tricky part is needing to set the height to 0 so it actually uses the
layout_weight
.Caveat: It's late and I've had a couple glasses of wine. :)