Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can't I override layout properties of included layouts in my Android project?

Tags:

android

layout

I am attempting to create some modular UI elements as described at Android Developers - Creating Reusable UI Components, unfortunately I'm not getting the desired result. The article states:

you can override all the layout parameters. This means that any android:layout_* attribute can be used with the tag.

My XML code looks like this (relevant parts):

<?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">

<include android:id="@+id/radio_group"
         layout="@layout/radio_buttons"
         android:layout_marginTop="75dp"
        />

<include android:id="@+id/player_group"
         layout="@layout/player_buttons"
         android:layout_alignParentBottom="true"
        />


<EditText android:id="@+id/text_field"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Your Login ID:"
    android:layout_below="@id/player_group"
    />

Here is the first of the included layouts:

<RadioGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radio_group"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    >

<RadioButton android:id="@+id/radio01"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Song #1"
             android:paddingLeft="45dp"
        />

[two more identical buttons]

<TextView   android:id="@+id/choice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="you have selected:"/>

</RadioGroup>

and the other:

<?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="wrap_content">

<Button android:text="btn01"
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />
<Button android:text="@+id/Button02"
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        />

<Button android:text="Start Playing"
        android:id="@+id/startPlayerBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:onClick="doClick"
        />


</LinearLayout>

Unfortunately - this results in all the included UI elements bunched together on top of each other at the top of the screen. The EditText element aligns correctly, but none of the included elements will respond to any overrided attributes. I have tried a number of different ones with the same non-results.

Note: I also tried putting an android:layout_alignParentBottom="true" parameter into the included layouts thinking that it might need to be in there for it to be overridden, but got the same non-result.

also - the marginTop param doesn't do anything either.

So - any idea what I'm doing wrong?? This is driving me crazy!

Note: there was a question asked a little while ago about how to style included layouts, the answer was a reference to the page on Reusable UI Components. Just want to say that I have read that question and the answer and it hasn't helped.

like image 938
Bennett Von Bennett Avatar asked Oct 23 '11 05:10

Bennett Von Bennett


1 Answers

There is a bug when using include where included layouts are not positioned correctly unless you specify values for android:layout_width and android:layout_height. Try this:

<?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">

    <include android:id="@+id/radio_group"
         layout="@layout/radio_buttons"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="75dp"
        />

    <include android:id="@+id/player_group"
         layout="@layout/player_buttons"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_below="@id/radio_group"
        />

    <EditText android:id="@+id/text_field"
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Your Login ID:"
        android:layout_below="@id/player_group"
        />
like image 65
goto10 Avatar answered Sep 22 '22 12:09

goto10