我有一个自定义视图组,它扩展
LinearLayour
,使用
<merge>
标记并引用此布局文件中的视图。
自定义视图组.java
public class CustomViewGroup extends LinearLayout {
public CustomViewGroup(final Context context, @Nullable final AttributeSet attrs) {
super(context, attrs);
inflate(context, R.layout.view_custom, this);
this.<TextView>findViewById(R.id.first).setText("Hello");
this.<TextView>findViewById(R.id.second).setText("World");
}
}
视图_自定义.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
而不是使用
findViewById
,我希望使用数据绑定库来生成对视图的引用,但是该库不支持
<合并>
LinearLayout
到XML布局文件中,并使
CustomViewGroup
延伸
FrameLayout
public class CustomViewGroup extends LinearLayout {
public CustomViewGroup(final Context context, @Nullable final AttributeSet attrs) {
super(context, attrs);
final ViewCustomBinding binding = ViewCustomBinding.inflate(LayoutInflater.from(context), this, true);
binding.first.setText("Hello");
binding.second.setText("World");
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
这个解决方案有一个缺点-现在视图层次结构更深,因为有一个冗余
框架布局
.
是否有一种方法可以使用具有所有优点的数据绑定库
<合并>
标记,即不使视图层次结构更深入?