DataBinding: How to get resource by dynamic id?

前端 未结 6 1903
予麋鹿
予麋鹿 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:29

    I ended up creating my own method:

    public class BindingUtils {
    
        public static String string(int resourceId) {
            return MyApplication
                    .getApplication()
                    .getResources()
                    .getString(resourceId);
        }
    
    }
    

    Declaring an import for it:

    
    
        
    
        ...
    
    
    

    And just calling it during binding:

    android:text="@{@string/myFormatString(BindingUtils.string(myPojo.resourceId))}"
    

    Would be nice to have out-of-the-box method for that. DataBinding is sitll in Beta - so maybe it will come in future.

提交回复
热议问题