如何在布局中使用类字段的常量值?

时间:2018-02-14 08:08:09

标签: android android-layout class-constants

我想在我的XML布局中使用一些java类常量值。这可能吗?

java class

public class ConstantKey {

   public static final String COUNTRY_CODE            = "+00";
   public static final STRING COUNTRY_NAME            = "USA";

}

Xml布局

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="ConstantKey.COUNTRY_NAME"/>

1 个答案:

答案 0 :(得分:2)

你可以:

1)如果字符串一直都是常量,你可以将它们作为字符串资源添加到strings.xml中。这样就可以从布局或java代码中访问它们。

2)如果值取决于网络呼叫或动态计算,我建议在andorid中使用DataBinding,请查看我的答案here以获取小型实施示例。