包括具有自定义属性的布局 [英] Include layout with custom attributes

查看:32
本文介绍了包括具有自定义属性的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个复杂的布局,我想为我的自定义组件使用 include 标签,如下所示:

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

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

topbar 布局具有自定义根组件,并且在布局 xml 文件中定义如下:

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

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

现在,我想将自定义定义的属性传递给topbar";像这样:

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

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

然后在自定义组件代码或理想情况下在 xml 中获取这些自定义属性的值.

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

遗憾的是,我无法获得 txt:trName 属性的值以使其成为 topbar 布局,我只是在代码中没有收到任何内容.如果我从 那个文档页面正确理解,我可以设置没有通过 include 使用的布局属性,而是 idheightwidth.

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.

所以我的问题是如何将我的自定义属性传递给通过 include 添加的布局?

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

推荐答案

我知道这是一个老问题,但我遇到了它并发现现在可以通过数据绑定实现.

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

首先您需要启用数据绑定在你的项目中.使用 DataBindingUtil.inflate(而不是 setContentView,如果它是 Activity)使其工作.

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>

这篇关于包括具有自定义属性的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆