Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with my include in RelativeLayout?

I want to create an activity with a title bar at top and a navigation bar at bottom. I used include to include the title bar layout and the navigation bar layout into the main layout as you can see below. The result is that both the title bar and navigation bar go to the top of the screen. Could someone tell me why? Thanks!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_widget" android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/background">

    <include android:id="@+id/title_bar" layout="@layout/title_bar" 
        android:layout_alignParentTop="true" />

    <include android:id="@+id/navigation_bar" layout="@layout/navigation_bar" 
        android:layout_alignParentBottom="true"/> 
</RelativeLayout>

[Edit] I didn't find the root cause. But the following works:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_widget" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="@drawable/background">

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"
    android:id="@+id/title_bar" >

    <include layout="@layout/title_bar" />
</RelativeLayout>

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true"
    android:id="@+id/navigation_bar" >

    <include layout="@layout/navigation_bar" />
 </RelativeLayout>
like image 966
user256239 Avatar asked Jul 09 '10 23:07

user256239


People also ask

Is RelativeLayout deprecated?

relativelayout is deprecated now.

What is Android RelativeLayout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

How do I set center in RelativeLayout?

RelativeLayout attributes You can change layout_align parent like top, left, and right. android:layout_centerHorizontal : This attribute is used to center the element horizontally within its parent container. You can change layout_center like layout_centerVertical or layout_centerInParent .

What is linear layout and relative layout in Android?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.


1 Answers

In order to override the attributes of the layout you're including, you must also override both the layout width and layout height. If both of those settings are not overridden, any other layout changes you try will be ignored.

Your layout above

<include android:id="@+id/title_bar" layout="@layout/title_bar" 
    android:layout_alignParentTop="true"
/>
<include android:id="@+id/navigation_bar" layout="@layout/navigation_bar" 
    android:layout_alignParentBottom="true"/>

Should actually be with a wrap content or fill parent, as appropriate.

<include android:id="@+id/navigation_bar" layout="@layout/navigation_bar"  android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>
like image 94
JLK Avatar answered Oct 19 '22 02:10

JLK