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 ?
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With