Android RelativeLayout and height of wrap_content?

て烟熏妆下的殇ゞ 提交于 2021-02-18 03:17:15

问题


I am trying to make a selection ListActivity, similar to the one used to add shortcuts to the launcher screens. I have rolled my own header and footers, which I would like to be "sticky" at the top and bottom of the view when on screen. In order to do this, I am using a RelativeLayout with the header set to dock to top, footer set to dock to bottom, and the list set to go below the header and above the footer. In terms of the overall layout of the activity, this is rendering as I would expect. The header is sticky to the top, the footer is sticky to the bottom, and the list scrolls in between them.

One odd thing though happened when I switched to the RelativeLayout as my root. Please see the following screenshot:

enter image description here

I want my Activity's height to be wrap_content, so that the form is only as high as the content displayed in it, but once i switched to RelativeLayout, it seems to render the Activity effectively as fill_parent, taking up the whole screen, even though the content doesn't warrant it. Notice that there are not enough list items to fill the screen, which with the fill_parent style, is leaving a bunch of whitespace between the end of the list, and the footer. I was setting my height's via styles - which worked fine with LinearLayout, but seems to be ignored now. So I tried hard-coding the height directly on the RelativeLayout, but it still doesn't work and still renders as fill_parent.

Here is my layout code:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                    style="@style/GroupsList"  
                    android:layout_height="wrap_content">

        <FrameLayout android:layout_height="wrap_content" 
                     android:layout_width="fill_parent" 
                     android:id="@+id/hdrGroups" 
                     android:layout_alignParentTop="true">
            <include layout="@layout/title" 
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content">
            </include>
        </FrameLayout>

        <FrameLayout style="@style/MyFooter" 
                     android:layout_height="wrap_content" 
                     android:layout_width="fill_parent" 
                     android:layout_alignParentBottom="true" 
                     android:id="@+id/ftrGroups">
            <ImageView style="@style/CloseButton" 
                       android:layout_height="wrap_content" 
                       android:layout_width="wrap_content" 
                       android:src="@drawable/add" 
                       android:id="@+id/imgGroupsAdd" 
                       android:clickable="true">
            </ImageView>
        </FrameLayout>

        <ListView android:divider="#9f9f9f" 
                  android:id="@+id/android:list" 
                  android:choiceMode="singleChoice" 
                  android:dividerHeight="1dp" 
                  android:layout_height="wrap_content" 
                  android:layout_width="fill_parent" 
                  android:layout_below="@id/hdrGroups" 
                  android:layout_above="@id/ftrGroups">
        </ListView>

        <TextView android:text="@string/browser_no_groups" 
                  style="@style/ListedItemB" 
                  android:id="@+id/android:empty" 
                  android:layout_height="wrap_content" 
                  android:layout_above="@id/ftrGroups" 
                  android:layout_below="@id/hdrGroups" 
                  android:layout_width="fill_parent">
        </TextView>
    </RelativeLayout>

All layout is done via XML, ... I am not doing any layout in code. How can I get the sticky header and footer while also having the activity as a whole behave in a wrap_content mode for its height? Is there some other way I should be going about this instead of a RelativeLayout?


回答1:


According to the developer documentation your desired behaviour is not possible for Relative layout ;

"Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM."

RelativeLayout

To solve your problem you could maybe try to use a linear layout, set to wrap_content and set max height via code to screen height.

You can get the screen height as described here : get screen height




回答2:


I just change my RelativeLayout in FrameLayout and all starts to work



来源:https://stackoverflow.com/questions/5959608/android-relativelayout-and-height-of-wrap-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!