Android的RelativeLayout和按钮问题
我有一个布局,可以在纵向模式下按我想要的方式工作。但是,它在横向模式下无法按我想要的方式显示:
这是 .xml 文件的轮廓:
<RelativeLayout>
<LinearLayout android:orientation="horizontal">
<TextView />
<TextView />
</LinearLayout>
<TextView />
<LinearLayout android:orientation="vertical">
<LinearLayout android:orientation="horizontal">
<LinearLayout android:orientation="vertical">
<TextView />
<TextView />
</LinearLayout>
<LinearLayout android:orientation="vertical">
<Spinner />
<Spinner />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button/>
</RelativeLayout>
这是实际的 .xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/departure_block"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="40px">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="17sp"
android:text="You are departing from " />
<TextView android:id="@+id/editTextDepartureStation"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="" android:layout_weight="2" android:textSize="17sp" />
</LinearLayout>
<TextView android:id="@+id/info_block" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="17sp"
android:layout_below="@id/departure_block" android:text="Please select remaining journey details: "
android:layout_marginTop="40px" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_below="@id/info_block"
android:layout_height="fill_parent">
<!-- Labels and widgets -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="20px">
<!-- Labels -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="2" android:paddingBottom="10px">
<TextView android:layout_width="wrap_content"
android:layout_height="70px" android:text="@string/destinationStation"
android:layout_weight="2" android:paddingTop="15px"
android:textSize="17sp" />
<TextView android:layout_width="wrap_content"
android:layout_height="70px" android:text="@string/numTickets"
android:layout_weight="2" android:paddingTop="15px"
android:textSize="17sp" />
</LinearLayout>
<!-- End labels -->
<!-- Widgets -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<Spinner android:id="@+id/spinnerDestinationStation"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:prompt="@string/destination_prompt" android:layout_weight="1"
android:textSize="17sp" />
<Spinner android:id="@+id/spinnerNumTickets"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:prompt="@string/numTickets_prompt" android:layout_weight="1"
android:textSize="17sp" />
</LinearLayout>
<!-- End widgets -->
</LinearLayout>
<!-- End labels and widgets -->
</LinearLayout>
<Button android:id="@+id/buttonPurchase" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Purchase ticket"
android:layout_weight="1" android:textSize="17sp" android:background="@drawable/custom_button"
android:textColor="#ffffff" android:shadowColor="#000000"
android:shadowRadius="1.5" android:layout_gravity="bottom"
android:layout_alignParentBottom="true" android:shadowDx="1"
android:shadowDy="1" android:layout_margin="10px" />
</RelativeLayout>
我明白为什么会出现该按钮在旋转器的顶部(它与父RelativeLayout的底部正确对齐),但我无法创建可以阻止重叠问题的xml。
我想要任何滚动或 ScrollView 来封装按钮。如果按钮出现在显示屏底部之外也不是问题 - 只要可以通过滚动访问它即可。这就是我努力实现的目标。
有人可以帮忙吗?
非常感谢。
I have a layout working as a I would like in portrait mode. However it does not display as I would like on landscape mode:
Here is an outline of the .xml file:
<RelativeLayout>
<LinearLayout android:orientation="horizontal">
<TextView />
<TextView />
</LinearLayout>
<TextView />
<LinearLayout android:orientation="vertical">
<LinearLayout android:orientation="horizontal">
<LinearLayout android:orientation="vertical">
<TextView />
<TextView />
</LinearLayout>
<LinearLayout android:orientation="vertical">
<Spinner />
<Spinner />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Button/>
</RelativeLayout>
And here is the actual .xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/departure_block"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="40px">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="17sp"
android:text="You are departing from " />
<TextView android:id="@+id/editTextDepartureStation"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="" android:layout_weight="2" android:textSize="17sp" />
</LinearLayout>
<TextView android:id="@+id/info_block" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="17sp"
android:layout_below="@id/departure_block" android:text="Please select remaining journey details: "
android:layout_marginTop="40px" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_below="@id/info_block"
android:layout_height="fill_parent">
<!-- Labels and widgets -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="20px">
<!-- Labels -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="2" android:paddingBottom="10px">
<TextView android:layout_width="wrap_content"
android:layout_height="70px" android:text="@string/destinationStation"
android:layout_weight="2" android:paddingTop="15px"
android:textSize="17sp" />
<TextView android:layout_width="wrap_content"
android:layout_height="70px" android:text="@string/numTickets"
android:layout_weight="2" android:paddingTop="15px"
android:textSize="17sp" />
</LinearLayout>
<!-- End labels -->
<!-- Widgets -->
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1">
<Spinner android:id="@+id/spinnerDestinationStation"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:prompt="@string/destination_prompt" android:layout_weight="1"
android:textSize="17sp" />
<Spinner android:id="@+id/spinnerNumTickets"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:prompt="@string/numTickets_prompt" android:layout_weight="1"
android:textSize="17sp" />
</LinearLayout>
<!-- End widgets -->
</LinearLayout>
<!-- End labels and widgets -->
</LinearLayout>
<Button android:id="@+id/buttonPurchase" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Purchase ticket"
android:layout_weight="1" android:textSize="17sp" android:background="@drawable/custom_button"
android:textColor="#ffffff" android:shadowColor="#000000"
android:shadowRadius="1.5" android:layout_gravity="bottom"
android:layout_alignParentBottom="true" android:shadowDx="1"
android:shadowDy="1" android:layout_margin="10px" />
</RelativeLayout>
I understand why the button is appearing on top of the spinners (it is correctly align to the bottom of the parent RelativeLayout), but I just can't create the xml which would stop the overlap problem.
I would like any scrolling or ScrollView to encapsulate the button. It is not a problem if the button appears off the bottom of the display - as long as it can be accessed by scrolling. This is what I struggled to achieve.
Can anyone help?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的布局对于屏幕来说太大,因此您必须将
info_block
包装在ScrollView
中,并将其与顶部对齐,并将其底部与底部对齐按钮的顶部。这意味着您必须在 info_block 之前定义按钮,以便可以将 info_block 的底部与按钮的顶部对齐。
为了澄清,问题是你的 info_block 的底部边缘没有绑定到任何东西,所以绘制按钮,你将有一个边缘可以靠着。您应该始终首先在相对布局中绘制固定元素,以便为未绑定元素提供边缘的位置。
Your layout is too big for the screen, so you'll have to wrap the
info_block
in aScrollView
, and align that to the top, as well as its bottom to the top of the button.This means you'll have to define the Button before the info_block, so that you can align the info_block's bottom to the button's top.
To clarify, the problem is your info_block's bottom edge is not bound to anything, so draw the button and you will have an edge to lay against. You should always draw the fixed elements in a relative layout first to give position for the unbound elements to edge against.
也许您最好的选择是使相对布局可滚动,我相信它被称为?这样用户就可以用手指上下移动整个布局。
Maybe your best option would be to make the Relative Layout scroll-able I believe it's called? That way a user can use their finger to move the whole layout up and down.
我明天得检查一下,但 fillViewport 似乎是我想要的 ScrollView 属性。解释于:ScrollView 的便捷技巧。
编辑添加此确实解决了我的问题。两个重要的事情是:
要在 ScrollView 中使用 fillViewport="true",这可以确保子元素展开以填充显示。仅此还不够,接下来要确保:
按钮之前的最后一个元素具有 android:layout_weight="1.0" 以确保它填充由具有 fillViewport 的 ScrollView 创建的空间="true"。
I will have to check tomorrow, but fillViewport seems to be the ScrollView attribute I want. Explained in: ScrollView’s handy trick.
Edited to add this did resolve my issue. The two important things are:
To use fillViewport="true" in the ScrollView, this ensures the child element expands to fill the display. This alone won't be enough, the next thing to ensure:
The last element before the Button has android:layout_weight="1.0" to ensure it fills the space created by the ScrollView having fillViewport="true".