使用 set 不会影响带有数据绑定的视图

Using set doesn't affect view with Data Binding

我正在尝试使用数据绑定更改不同的 EditText 属性,但它似乎不起作用。

custom_edittext.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="textVar"
            type="String"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/descriptionTextEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:ems="12"
            android:inputType="textMultiLine"
            android:text="@{textVar}"/>
    </RelativeLayout>
</layout>

我的 class 扩展了 RelativeLayout

public class CustomEditText extends RelativeLayout {
public CustomEditText(Context context) {
        super(context);
        init(context, null);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
}

和我的初始化:

private void init(Context context, AttributeSet attrs){
         View.inflate(context, R.layout.custom_edittext, this);

         CustomEdittextBinding stomEdittextBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custom_edittext, null, false);

         Drawable drawableButton = context.getResources().getDrawable(R.drawable.ic_action_name);//context.getResources().getDrawable(R.drawable.ic_action_name);
         customEdittextBinding.setTextVar("new text"); //it should change the text, shouldn't it?
         }

我的主要activity: @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    customEditText = findViewById(R.id.custom);
}

并且在 activity_main.xml 中我已经像这样添加了我的 CustomEditText 但是有一个错误 - 找不到以下 classes。

<com.example.xxx.app.CustomEditText
        android:id="@+id/custom"
        android:layout_width="344dp"
        android:layout_height="50dp"
        android:paddingLeft="100dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp"></com.example.xxx.app.CustomEditText >

好的,我已经通过在实例化 CustomEdittextBinding 后添加 addView(customEdittextBinding.getRoot()); 解决了这个问题。