相对布局导致格式问题
我最近一直在为我的学校开发一个应用程序,想在发布它之前对其进行一些清理。在应用程序的调度部分,我有 5 个按钮,它们可以同时在屏幕上的 ListView 上执行操作。但是,当屏幕上有大约 6 个或更多事件时,我会遇到问题,因为一旦列表视图接管屏幕并将按钮推离屏幕,我就无法删除事件、创建新事件等在。
我尝试将列表视图设置为适用于正常屏幕方向的静态大小(400px),但如果手机设置为横向视图,您也看不到按钮。使用我当前的代码,它似乎可以在 XML 查看器中工作,但实际上并非如此。
这是没有静态大小设置的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700"
>
<Button android:text="@string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button3">
</Button>
<Button android:text="@string/Edit"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button3"
android:layout_alignTop="@id/button3">
</Button>
<Button android:text="@string/delete"
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button4"
android:layout_alignTop="@id/button4">
</Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button7"
android:layout_width="wrap_content"
android:text="@string/Previousweek"
android:layout_below="@id/button3">
</Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button6"
android:layout_width="wrap_content"
android:text="@string/Next"
android:layout_below = "@id/button3"
android:layout_toRightOf = "@id/button7">
</Button>
<ListView android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="@id/button7"
android:textSize="10sp" >
</ListView>
</RelativeLayout>
此代码的 XML 查看器为:
这会让我相信它会正常工作。我在模拟器上进行了测试,并在输入了一堆愚蠢的事件后得到了以下结果:
这个结果与模拟器的多个版本一致。
如何在不使用导致横向问题的静态尺寸限制的情况下解决此问题?
I have been working on an app for my school recently and wanted to clean it up a bit before possibly publishing it. On the scheduling portion of the app, I have 5 buttons that perform actions on a ListView that is also on the screen at the same time. However, I have the issue when I have around 6 or more events on the screen as once the list view takes over the screen and pushes the buttons off the screen, making it so that I cannot delete the events, make new ones, and so on.
I tried setting the list view to a static size (400px) which worked for normal screen orientation, but if the phone is set to landscape view you cannot see the buttons either. With my current code it would appear to work in the XML viewer but in practice is not the case.
This is the code without the static size setting:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#551A8B"
android:textColor="#FFD700"
>
<Button android:text="@string/New"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button3">
</Button>
<Button android:text="@string/Edit"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button3"
android:layout_alignTop="@id/button3">
</Button>
<Button android:text="@string/delete"
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button4"
android:layout_alignTop="@id/button4">
</Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button7"
android:layout_width="wrap_content"
android:text="@string/Previousweek"
android:layout_below="@id/button3">
</Button>
<Button android:layout_height="wrap_content"
android:id="@+id/button6"
android:layout_width="wrap_content"
android:text="@string/Next"
android:layout_below = "@id/button3"
android:layout_toRightOf = "@id/button7">
</Button>
<ListView android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFD700"
android:layout_below="@id/button7"
android:textSize="10sp" >
</ListView>
</RelativeLayout>
The XML viewer for this code is:
Which would lead me to believe it would work fine. I tested it on my emulator and got the following result after entering a bunch of silly events however:
This result is consistent with multiple versions of the emulator.
How I can fix this problem without using static size constraints that cause landscape orientation issues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将按钮分离到单独的
RelativeLayout
中,并将其和ListView
包含在垂直的LinearLayout
中。然后:
这里的关键点是
ListView
上的高度和重量。这意味着在为按钮正确分配空间后,它会填充 LinearLayout 中的剩余空间。Separate the buttons into a separate
RelativeLayout
and enclose this and theListView
in a verticalLinearLayout
.Then:
The key point here is the height and weight on the
ListView
. This means that it fills the remaining space in theLinearLayout
after space has been correctly allocated for the buttons.在 listView 标记中添加 android:weigth 并将 android:weigth 值设置为 1。当列表视图高度和宽度设置为 fill_parent 并且列表视图覆盖整个布局时,这将起作用。所以尝试一下,它会起作用的。
Add a android:weigth in your listView tag and set the android:weigth value to 1. This will work when your list view height and width is set to fill_parent and your list view is covering entire layout. So try it, it will work.
一种简单的解决方案是将按钮按其自己的相对布局分开,并将整个按钮放在线性布局中,例如:
One simple solution would be to separate the buttons in their own relative layout and put the whole thing in a linear layout, eg:
使用具有两行按钮的垂直 LinearLayout(每行作为 LinearLayout),然后为 ListView 指定layout_weight 值为“1”。事实上,也可以使用layout_weight来清理按钮的大小。
Use a vertical LinearLayout with two rows of Buttons (each row as a LinearLayout), then give the ListView a layout_weight value of "1". In fact, use layout_weight to clean up the size of your buttons too.