DataBinding: How to get resource by dynamic id?

前端 未结 6 1923
予麋鹿
予麋鹿 2020-12-24 02:12

I know that it is possible to reference resources in layout by their resource id:

android:text=\"@{@string/resourceName}\"

However, I would

6条回答
  •  没有蜡笔的小新
    2020-12-24 02:28

    Another solution if you already have Context defined in your xml then you will not need to import String class.

    android:text="@{@string/myFormatString(context.getString(pojo.res))}"
    

    will work for

    Value is: %s
    

    If you don't have context in your xml. then follow this

        
         
    
         
     
    

    and in your Activity

    ActivityUserBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_user);
    binding.setPojo(new MyPOJO());
    binding.setContext(this);
    

提交回复
热议问题