我是 Android 开发的新手。
是否可以使用自定义属性更改包含布局内的视图?
我的意思是这样的:
这是my_layout
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/userImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="..." />
<!-- this src attribute can be overrided using my own attribute in the iclude tage -->
<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="..." />
<!-- this text attribute can be overrided using my own attribute in the iclude tage -->
</LinearLayout>
这就是我想要包含my_layout
到活动布局中的方式:
<include layout="@layout/my_layout" userName="@string/userName1" userImage="@drawable/userImage1"/>
userName
并userImage
覆盖android:text
和android:src
属性的值。
我已经阅读了有关数据绑定的信息,但我的意思是不使用任何 Java 类。我只需要在数据标签中定义一个变量,my_layout
其中type
引用的值@string/
或@drawable/
获取文本或图片。
可能吗?