【问题标题】:Generated FragmentBinding does not contain my CustomView id生成的 FragmentBinding 不包含我的 CustomView id
【发布时间】:2017-11-01 11:34:45
【问题描述】:

我在我的 android 项目中使用数据绑定,而我的 dashboard_fragment_layout.xml 包含 LinearLayout,其中包含 TextView 和 CustomView:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.ui.dashboard.DashboardFragment">
    <data>
        <variable
            name="ViewModel"
            type="com.example.ui.dashboard.DashboardViewModel" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tvSome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <view
            android:id="@+id/viewCustom"
            class="com.example.ui.dashboard.CustomView"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|center_horizontal" />
    </LinearLayout>
</layout>

当我尝试通过生成的 FragmentDashboardBinding 访问我的自定义视图时:

mCustomView = mBinding.viewCustom;

我在 AndroidStudio 中收到“无法解析符号 viewCustom”。而且我对 TextView 没有这个问题,它可以从 mBinding 对象访问:

mSomeTextView = mBinding.tvSome;  // all fine, no errors

我总是在自定义视图中遇到此错误,访问我的自定义视图对象的唯一方法是使用 findViewById 以旧方式执行此操作:

mCustomView = view.findViewById(R.id.viewCustom); // that works

大家一起:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewDataBinding = DataBindingUtil.inflate(inflater, fragment_dashboard_layout, container, false);
        view = mViewDataBinding.getRoot();

        mCustomView = mViewDataBinding.viewCustom;   // 'Can't resolve symbol viewCustom
        mTextView = mViewDataBinding.tvSome;  // all fine
        mCustomView = view.findViewById(R.id.viewCustom);  // that works
    }

如何通过生成的数据绑定对象访问自定义视图?

【问题讨论】:

    标签: android android-custom-view android-databinding android-viewmodel


    【解决方案1】:

    在xml代码中改为com.example.ui.dashboard.CustomView

    并删除xml代码中的类。

    试试这个。

    <com.example.ui.dashboard.CustomView
        android:id="@+id/viewCustom"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|center_horizontal" />
    

    【讨论】:

      【解决方案2】:

      您使用了错误的视图标签。尝试将 view 标签替换为 View 将您的视图代码替换为此。

      <View
              android:id="@+id/viewCustom"
              class="com.example.ui.dashboard.CustomView"
              android:layout_width="300dp"
              android:layout_height="match_parent"
              android:layout_gravity="bottom|center_horizontal" />
      

      编码愉快!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-13
        • 1970-01-01
        • 2016-05-06
        • 2021-10-25
        相关资源
        最近更新 更多