Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

android - Include layout with custom attributes

I'm building a complex layout and I want to use include tag for my custom component, like this:

<include layout="@layout/topbar"/>

The topbar layout has custom root component and in layout xml file it's defined like this:

<my.package.TopBarLayout
 ... a lot of code

Now, I wanna pass my custom defined attributes to "topbar" like this:

<include layout="@layout/topbar" txt:trName="@string/contacts"/>

And then get the value of those custom attributes in custom component code or ideally in xml.

Sadly, I cannot get value of txt:trName attribute to make it to the topbar layout, I just don't receive anything in code. If I understand correctly from that documentation page, I can set no attributes for layouts used via include, but id, height and width.

So my question is how can I pass my custom defined attributes to layout which is added via include?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this is an old question but I came across it and found that it is now possible thanks to Data Binding.

First you need to enable Data Binding in your project. Use DataBindingUtil.inflate (instead of setContentView, if it's Activity) to make it work.

Then add data binding to the layout you want to include:

<?xml version="1.0" encoding="utf-8"?>    
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="title" type="java.lang.String"/>
    </data>
    <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/screen_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="center">

    ...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="20sp"
            android:textStyle="bold"
            android:text="@{title}"/>

    ...

    </RelativeLayout>
</layout>

Finally, pass the variable from the main layout to the included layout like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        ...
    </data>
    ...
    <include layout="@layout/included_layout"
        android:id="@+id/title"
        app:title="@{@string/title}"/>
    ...
</layout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...