【问题标题】:setting a customView variable while databinding在数据绑定时设置 customView 变量
【发布时间】:2019-08-02 17:31:20
【问题描述】:

我正在尝试从 main_activity.xml 设置 customView 的变量(buttonText),但出现此错误:

属性 buttonText (aka com.example.testbinding:buttonText) 不是 成立。错误:链接文件资源失败。

customView.xml

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

    <LinearLayout
        android:id="@+id/container"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:text="@{buttonText}" />
    </LinearLayout>
</layout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    <data>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.example.testbinding.CustomView
            android:id="@+id/customView"
            ...
            app:buttonText = "Test button text"
            ...
        >

        </com.example.testbinding.CustomView>
    </android.support.constraint.ConstraintLayout>
</layout>

在这个post我发现:

在您的自定义视图中,按照您通常的方式扩展布局,并且 为您要设置的属性提供一个 setter:

我的 CustomView 类有一个用于 buttonText 属性的公共设置器:

    public class CustomView extends LinearLayout {

        private CustomViewBinding binding;
        private String buttonText;

        public CustomView(Context context) {
            super(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            binding = DataBindingUtil.inflate(inflater, R.layout.custom_view, this, true);
        }

        public String getButtonText() {
            return buttonText;
        }

        public void setButtonText(String buttonText) {
            this.buttonText = buttonText;
        }
    }

谁能告诉我它有什么问题? 还是没有 BindingAdapter 就不可能?

【问题讨论】:

    标签: android android-databinding


    【解决方案1】:

    binding.setbuttonText("text_button");

    而不是

        public String getButtonText() {
            return buttonText;
        }
    
        public void setButtonText(String buttonText) {
            this.buttonText = buttonText;
        }
    

    【讨论】:

    • 谢谢,但这里有问题: 我无法从父 xml 设置此属性。
    • 你可以尝试使用 binding.setbuttonText("text_button"); 在 java 文件中设置值吗?而不是在 xml 文件中编写代码? @AndreyZyritel
    【解决方案2】:

    似乎我发现了问题所在。

    如果我使用在父视图中声明的变量 - 一切正常。但是如果我在属性中使用一个值,它就行不通了。

    错误:

    app:buttonText = "Test button text"
    app:progressValue = "50"
    

    正确:

    <variable
    name="viewModel"
    type="com.example.testbinding.ViewModel"/>
    
    app:buttonText = "@{viewModel.buttonText}"
    app:progressValue = "@{viewModel.progressValue}"
    

    当然,viewModel 必须为这些属性定义一个设置器。

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多