我一直在试图创建一个RelativeLayout头、身体(名单)和页脚。我希望头上的一切(结盟顶),身体(名单)以下标题(滚动时)和脚注(对准到底)以上的体(名单)-只有当它的可见。

我已经成功地这样做完美的时候事情怎么看但是很显然的XML布局造成的问题我已经有点击时listItems(看看这里, 和这里).

为了解决该问题,我不得不包裹的列表视图与LinearLayout,并把它的头部和脚注(在XML)。这解决了listItems击问题,但创建一个新的问题的脚注隐藏的最后一列表视图项目,尽管列出并在头滚动时下来。

我非常绝望的在这里:\

这是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>

我感谢所有帮助以某种方式找到一个解决这两个问题!

谢谢你!

有帮助吗?

解决方案

我可以问你为什么使用RelativeLayout在所有容器?这似乎是一个完美的地方就使用LinearLayout与加权的中心地区(列表视图)设置采取的所有剩余的区域后wrap_height设置的头部和尾。RelativeLayouts是巨大的,用于简化深深地嵌套的布局,其中包含水平和垂直两个要素,但是你的外层在这里,只有3垂直的元素不管怎么说,这样下去有LinearLayout与 orientation:vertical 除非我失去了一些东西在这里。

编辑:总之,试试这个;这应该给你一个固定的高度头,一个包裹高脚,并且将中央区占所有剩余空间。确保外布置 fill_parent 高度和中部地区被设置为一个固定的高度,但有一个 layout_weight="1".我喜欢使用"0px"为基layout_height当我使用layout_weight只是为了让我清楚当我回去和再读它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    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"
        > <!-- snip - inner contents unchanged -->
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/listArea"
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:orientation="horizontal"
        ><!-- snip - inner contents unchanged -->
    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_control_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/top_background"
        android:orientation="horizontal"
        android:visibility="visible"
        ><!-- snip - inner contents unchanged -->
    </LinearLayout>
</LinearLayout>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top