我需要实现自己的属性,如in com.android.R.attr
在官方文档中找不到任何内容,因此我需要有关如何定义这些attrs以及如何在我的代码中使用它们的信息.
我想在同一个视图中多次使用我的一个布局include.假设我有custom.xml一些包含TextViews.
custom.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我已将此布局多次包含在parent.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/custom"
android:id="@+id/layout1"/>
<include layout="@layout/custom"
android:id="@+id/layout2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在我想将我的数据模型绑定到这个布局,但问题是我不知道如何绑定两个不同的数据模型layout1,layout2因为它们都是从一个布局中引用的custom.xml.据我所知,我可以在我的xml布局中添加此标记:
<data>
<variable name="user" type="com.myproject.model.User"/>
</data>
Run Code Online (Sandbox Code Playgroud)
但我需要绑定两个不同的数据模型custom.xml.
我的问题是如何在一个视图中多次包含布局并使用数据绑定将不同的数据传递给它们?比如将数据传递给布局但不是将模型静态绑定到xml.
我也发现这个问题确实存在同样的问题但是由于数据绑定是在较新版本的android中发布的,我正在寻找一种方法来解决使用数据绑定的相同问题.以下是我引用澄清的问题的一部分:
例如,我有一个精心设计的布局,我希望在我的视图中显示三次.每个实例都需要不同的值.由于
include基本上是XML并将其粘贴到此处,因此我需要更强大的功能.