当我开始开发Android应用程序时,我倾向于在任何需要的地方定义自定义R值,特别是在布局文件中.例如:
findViewById(R.id.customerName).setText(customer.getName())
Run Code Online (Sandbox Code Playgroud)
布局:
<TextView android:text="TextView" android:id="@id/customerName"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
现在我意识到,使用它可能会更好android.R.
findViewById(android.R.id.text1).setText(customer.getName())
Run Code Online (Sandbox Code Playgroud)
布局:
<TextView android:text="TextView" android:id="@android:id/text1"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
你遵循什么样的做法?各有哪些优缺点?
我有两个Android项目,一个主项目(包名称com.adip.sampler)和一个添加到main(包名称com.samples.projb)的库.在两个资源中我都有一个integer-array相同的密钥my_int_values:
在主项目中:
<integer-array name="my_int_values">
<item>10</item>
<item>20</item>
<item>30</item>
<item>40</item>
<item>50</item>
<item>60</item>
<item>70</item>
<item>80</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)
在图书馆:
<integer-array name="my_int_values">
<item>34</item>
<item>35</item>
<item>36</item>
<item>37</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)
在活动的主项目中,如果我正在研究这些数组(主项目和库)的值是什么:
protected void showLocalStrings() {
Log.d("RESSampler", "In Main: " + Arrays.toString(getResources().getIntArray(com.adip.sampler.R.array.my_int_values)));
Log.d("RESSampler", "In Libr: " + Arrays.toString(getResources().getIntArray(com.samples.projb.R.array.my_int_values)));
}
Run Code Online (Sandbox Code Playgroud)
然后我在Logcat中看到了这个:
In Main: [10, 20, 30, 40, 50, 60, 70, 80]
In Libr: [10, 20, 30, 40, 50, 60, 70, 80]
Run Code Online (Sandbox Code Playgroud)
似乎主项目覆盖了库数组中定义的值...如果我正在使用正确的密钥从资源中读取,那么我加倍检查了.直到我看了每个生成的R类.在主项目中,这就是我所拥有的com.adip.sampler.R.array.my_int_values:
public static final class array {
public …Run Code Online (Sandbox Code Playgroud)