Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Re-using Layouts with <include/> with parameters?

Tags:

android

I had a problem ,that is I have some ViewGroup,which have almost the same content. like this:

<LinearLayout>     <TextView android:text="aaa"/>     <ImageView android:src="bbb"/> </LinearLayout>  <LinearLayout>     <TextView android:text="ccc"/>     <ImageView android:src="ddd"/> </LinearLayout> 

I want to convert to this:

<include layout="xxLayout"     param1="aaa"     param2="bbb"/> <include layout="xxLayout"     param1="ccc"     param2="ddd"/>   xxLayout.xml <LinearLayout>     <TextView android:text="{param1}"/>     <ImageView android:src="{param2}"/> </LinearLayout> 

I know I can do it use java code.Could it be possible that just use xml ?

like image 872
Fan Applelin Avatar asked Apr 14 '16 08:04

Fan Applelin


People also ask

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. For example, a yes/no button panel, or custom progress bar with description text.

How do I change the layout of my Android phone?

Convert a view or layoutClick the Design button in the top-right corner of the editor window. In the Component Tree, right-click the view or layout, and then click Convert view.... In the dialog that appears, choose the new type of view or layout, and then click Apply.

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

This is possible using only xml, thanks to the databinding library:

First wrap xxLayout.xml in a layout and add a databinding class definition that lists the variables you want to pass into the included layout.

<layout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">      <data>         <variable name="myText" type="java.lang.String"/>         <variable name="mySrc" type="android.graphics.drawable.Drawable"/>     </data>      <LinearLayout>         <TextView android:text="@{myText}"/>         <ImageView android:src="@{mySrc}"/>     </LinearLayout> </layout> 

Then use the databinding library to inflate the including layout and inject the desired values using databinding statements. See the databinding library documentation for details on the databinding expression language and what it supports.

<layout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">      <LinearLayout>         <include layout="xxLayout"             app:myText="@{`aaa`}"             app:mySrc="@{@drawable/bbb}"/>         <include layout="xxLayout"             app:myText="@{`ccc`}"             app:mySrc="@{@drawable/ddd}"/>     </LinearLayout> </layout> 
like image 95
M.Pomeroy Avatar answered Oct 11 '22 15:10

M.Pomeroy