Android 解决数据绑定不支持合并标记的问题
我有一个自定义视图组,它扩展了Android 解决数据绑定不支持合并标记的问题,android,android-databinding,Android,Android Databinding,我有一个自定义视图组,它扩展了LinearLayour,使用标记扩展XML布局文件,并引用此布局文件中的视图 CustomViewGroup.java public class CustomViewGroup extends LinearLayout { public CustomViewGroup(final Context context, @Nullable final AttributeSet attrs) { super(context, attrs);
LinearLayour
,使用
标记扩展XML布局文件,并引用此布局文件中的视图
CustomViewGroup.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");
}
}
此解决方案有一个缺点-现在视图层次更深,因为有一个冗余的FrameLayout
有没有一种方法可以使用数据绑定库,它具有
标记的所有优点,即不加深视图层次结构?您所说的合并是什么意思???@AngusTay我的意思是这里描述的
标记:@pskink如果我将
替换为
,我将在CustomViewGroup
内部膨胀CustomViewGroup
太糟糕了,安卓团队两年后仍然没有对其进行开发。你觉得这个有用吗?:)
<?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>
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>