我想在我的XML布局中使用一些java类常量值。这可能吗?
public class ConstantKey {
public static final String COUNTRY_CODE = "+00";
public static final STRING COUNTRY_NAME = "USA";
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ConstantKey.COUNTRY_NAME"/>
答案 0 :(得分:2)
你可以:
1)如果字符串一直都是常量,你可以将它们作为字符串资源添加到strings.xml
中。这样就可以从布局或java代码中访问它们。
2)如果值取决于网络呼叫或动态计算,我建议在andorid中使用DataBinding
,请查看我的答案here以获取小型实施示例。