Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying nested attributes on an included Layout

I've been learning about merge and include lately, and I have a question I can't seem to figure out the answer too. Say I have a layout that defines a header component that I want to add to multiple layouts. However, I want to change the title, or icon of each header per each include usage. For example say I have the following layout:

<RelativeLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                style="@style/menu_header">

    <Button android:id="@+id/backButton"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/button"
            android:text="@string/back"/>

    <TextView style="@style/headerTitle"
              android:layout_centerInParent="true"
              android:text="${title}"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>
</RelativeLayout>

Then I can include that in other layouts using:

<LinearLayout ...>
   <include android:id="@+id/searchHeader" layout="@layout/shared_header" title="Search"/>
   ...
</LinearLayout>

I know I can modify any layout_* attribute of the root element, but can I define other attributes that get substituted into the layout, like say "title" in this example, without having to create my own subclass of View, add declare-styleable definitions in valaues/resources, etc?

Having something like this would make creating reusable views so much simpler, but I can't seem to find any evidence that says if merge + include can do it.

like image 975
chubbsondubs Avatar asked Jul 12 '11 18:07

chubbsondubs


People also ask

What is a nested Layout?

By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts.

Is it possible to include one Layout definition in another?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts.

Which Layout is best for large complex hierarchies?

Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance. The default maximum depth is 10.

How can use multiple layouts in one activity in Android?

You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.


1 Answers

The answer is nope. Unfortunately, Android isn't that powerful. You have to create your own extension of ViewGroup and write more code.

like image 122
chubbsondubs Avatar answered Sep 20 '22 06:09

chubbsondubs