【问题标题】:Data Binding in Android Custom ViewAndroid 自定义视图中的数据绑定
【发布时间】:2016-06-05 01:56:27
【问题描述】:

我创建了一个自定义布局,如下所示。我想用这个布局执行数据绑定。我该如何执行此任务。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
        <TextView
            android:id="@+id/no_internetConnection_Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_toRightOf="@+id/icon_noInternet_connection"
            android:text="@string/no_Internet_Connection_text" />
    </LinearLayout>
</layout>

这是现有的代码。如何用数据绑定代码替换现有代码。

public class NetworkConnectionCheck {
    private  Context _context;

    public NetworkConnectionCheck(Context _context) {
        this._context = _context;
    }

    public void CustomToastShow() {
        LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate( R.layout.customtoast, null );
        TextView text = (TextView) view.findViewById(R.id.no_internetConnection_Text);  
        ImageView imageView = (ImageView) view.findViewById(R.id.icon_noInternet_connection);
        Toast toast = new Toast(_context);
        toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(view);
        toast.show();
    }
}

目前我遇到了一个错误。这是因为 Layout 标签。

Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class layout

【问题讨论】:

标签: java android android-layout android-fragments


【解决方案1】:

我希望你已经在你的模块 gradle 文件中启用了 dataBinding。

android {
    ....
    dataBinding {
        enabled = true
    }
}

【讨论】:

  • 是的,我已经在模块 gradle 文件中启用了数据记录
  • 在我的情况下没有生成CustomtoastBinding类。我不在活动类或onCreate方法中。
【解决方案2】:

MyLayoutBinding 未生成,因为您的 xml 文件不包含数据标记。一旦你将数据标签放入布局中并在数据中声明一个变量 MyLayoutBinding 应该会生成

【讨论】:

    【解决方案3】:

    你需要添加

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
        <merge>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">
            <TextView
                android:id="@+id/no_internetConnection_Text"
                android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_toRightOf="@+id/icon_noInternet_connection"
            android:text="@string/no_Internet_Connection_text" />
    </LinearLayout>
    <merge/>
    </layout>
    

    在您的自定义视图中

    public class NetworkConnectionCheck {
        private  Context context;
    
        public NetworkConnectionCheck(Context context) {
            super(context);
            init();
        }
        public NetworkConnectionCheck(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
        public NetworkConnectionCheck(Context context,  AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        public void CustomToastShow() {
            binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.customtoast, this, true);
            //do what you need to do with the binding
        }
    }
    

    【讨论】:

      【解决方案4】:

      对于遇到相同问题的其他人,请检查:

      • XML 文件必须有布局标签
      • 绑定类名将根据 XML 文件名生成。 my_file.xml 将产生名称“myFileBinding”
      • 如果找不到绑定类,请尝试重建项目。

      【讨论】:

        猜你喜欢
        • 2016-04-20
        • 1970-01-01
        • 2018-10-13
        • 2013-01-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多