簡體   English   中英

Android數據綁定 - 錯誤:(119,29)標識符必須具有XML文件中的用戶定義類型。 main_radio_subscribe缺少它

[英]Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it

我一直試圖使用android數據綁定中的隱式屬性監聽器( 引用 )來控制視圖的可見性,它允許通過id和訪問屬性(如checked,visible等)訪問視圖,但是當嘗試使用它時,它會拋出像這樣的錯誤

Error:(119, 29) Identifiers must have user defined types from the XML file. addTodo_switch_remind is missing it
<android.support.v7.widget.SwitchCompat
        android:id="@+id/addTodo_switch_remind"
        style="@style/MediumTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addTodo_space_project"
        android:text="@string/add_todo_remind_label"
        android:textOff="@string/generic_no_text"
        android:textOn="@string/generic_yes_text" />

    <android.support.v4.widget.Space
        android:id="@+id/addTodo_space_remind"
        style="@style/FormsSpacingStyle"
        android:layout_below="@+id/addTodo_switch_remind" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/addTodo_space_remind"
        android:orientation="vertical"
        android:padding="@dimen/grid_box_single"
        android:visibility="@{addTodo_switch_remind.checked ? View.VISIBLE : View.GONE}">

當您在.xml文件中使用View.VISIBLE / View.GONE時,您應該通過在數據部分中添加<import type="android.view.View"/>來導入View類型,如下所示:

<data>
    <import type="android.view.View"/>

    <variable
        name="viewModel"
        type="xx.xx.MyViewModel"/>
</data>

看起來隱式屬性偵聽器在表達式中使用駝峰的情況下,感謝這篇文章我想出來了。

<!--Recurring Reminder -->
        <android.support.v7.widget.SwitchCompat
            android:id="@+id/addTodo_switch_remind"
            style="@style/MediumTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addTodo_space_project"
            android:text="@string/add_todo_remind_label"
            android:textOff="@string/generic_no_text"
            android:textOn="@string/generic_yes_text" />

        <android.support.v4.widget.Space
            android:id="@+id/addTodo_space_remind"
            style="@style/FormsSpacingStyle"
            android:layout_below="@+id/addTodo_switch_remind" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/addTodo_space_remind"
            android:orientation="vertical"
            android:padding="@dimen/grid_box_single"
            android:visibility="@{addTodoSwitchRemind.checked ? View.VISIBLE : View.GONE}">

記錄具有相同問題的其他人

第1步 :創建BindingAdapter

@BindingAdapter("android:visibility")
public static void setVisibility(final View view, @IdRes int layourId) {
    SwitchCompat switcher = (SwitchCompat)view.getRootView().findViewById(layourId)
    switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            view.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        }
    }
}

第2步 :在layout.xml中的數據綁定數據部分導入R類:

<data>
     <import type="example.package.R"/>
</data>

第3步 :將自定義視圖綁定到切換台,如下所示:

<android.support.v7.widget.SwitchCompat
    android:id="@+id/addTodo_switch_remind"/>

<LinearLayout
    android:visibility="@{R.id.addTodo_switch_remind">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM