Android:XML 布局问题
我一直在尝试创建一个带有标题、正文(列表)和页脚的相对布局。 我希望标题位于所有内容之上(与顶部对齐),正文(列表)位于标题下方(滚动时),页脚(与底部对齐)位于正文(列表)上方 - 仅当它可见时。
当涉及到事物的外观时,我已经成功地做到了这一点,但显然 XML 布局是我在单击 listItems 时遇到的问题的原因(参见此处,和这里)。
为了解决这些问题,我必须用 LinearLayout 包装 ListView 并将其放在页眉和页脚之间(在 XML 中)。 这解决了 listItems 单击问题,但创建了一个新问题 - 页脚隐藏了最后一个 ListView 项目,即使向下滚动时列表确实位于标题下方。
我在这里非常绝望:\
这是创建完美布局但导致列表项点击问题的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:visibility="invisible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_control_bar"
android:layout_above="@id/bottom_control_bar"
android:drawSelectorOnTop="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:cacheColorHint="#ffffff"
android:listSelector="@drawable/selected_item"
/>
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star."
android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" />
</RelativeLayout>
这是隐藏最后一个列表项但解决点击问题的 XML问题:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/top_control_bar"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ffffff" />
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star." />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_above="@id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
</RelativeLayout>
如果您能以某种方式找到这两个问题的解决方案,我将不胜感激!
谢谢你!
I've been trying to create a RelativeLayout with a header, body (list) and footer.
I want the header to be above everything (aligned to top), the body (list) to be under the header (when scrolling) and the footer (aligned to bottom) to be above the body (list) - only when it's visible.
I've managed to do so PERFECTLY when it comes to how things look but apparently the XML layout is the cause to the issues I've been having when clicking on listItems (see here, and here).
In order to solve the issues, I had to wrap the ListView with LinearLayout and put it between the header and the footer (in the XML).
This solves the listItems clicking issues BUT creates a new issue - the footer hides the last ListView item, even though the list does go under the header when scrolling it down.
I'm pretty much desperate here :\
This is the XML which creates the perfect layout but causes the List Item clicking issues:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:visibility="invisible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_control_bar"
android:layout_above="@id/bottom_control_bar"
android:drawSelectorOnTop="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:cacheColorHint="#ffffff"
android:listSelector="@drawable/selected_item"
/>
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star."
android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" />
</RelativeLayout>
This is the XML which hide the last List Item but solves the clicking issues:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/top_control_bar"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ffffff" />
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star." />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_above="@id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
</RelativeLayout>
I'd appreciate any help to somehow find a solution for both issues!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能问一下为什么你要为容器使用relativelayout吗?这似乎是一个完美的地方,只使用带有加权中心区域(列表视图)的 LinearLayout ,该区域被设置为占据在设置了 wrap_height 的页眉和页脚之后的所有剩余区域。 relativeLayouts 非常适合简化包含水平和垂直元素的深层嵌套布局,但这里的外层无论如何只包含 3 个垂直元素,所以请使用带有
orientation:vertical
的 LinearLayout 除非我遗漏了一些东西这里。编辑:简而言之,试试这个;这将为您提供固定高度的页眉、环绕高度的页脚以及占据所有剩余空间的中心区域。确保外部布局设置为
fill_parent
高度,中间区域设置为固定高度,但带有layout_weight="1"
。当我使用layout_weight时,我喜欢使用“0px”作为基本layout_height,只是为了在我稍后回去重读时保持清晰。Can I ask why you're using a RelativeLayout at all for the container? It seems like a perfect place to just use a LinearLayout with a weighted center area (the listview) that's set up to take all remaining area after the wrap_height-set header and footer. RelativeLayouts are great for simplifying deeply nested layouts which contain both horizontal and vertical elements, but your outer layer here only contains 3 vertical elements anyways, so go with a LinearLayout with
orientation:vertical
unless I'm missing something here.EDIT: In short, try this; this should give you a fixed height header, a wrapping-height footer, and the center area taking up all remaining space. Make sure the outer layout is set to
fill_parent
height and the middle area is set to a fixed height but with alayout_weight="1"
. I like to use "0px" for the base layout_height when I'm using layout_weight just to keep it clear to me when I go back and reread it later.