什么是更好的android.R或定制R' [英] What is better android.R or custom R?

查看:173
本文介绍了什么是更好的android.R或定制R'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始深化发展的Andr​​oid应用程序,我不得不在布局文件中定义自定义的R值,无论我所需要的,尤其是一个趋势。例如:

When I started developping android applications, I had a tendency to define custom R values wherever I need, in particular in layout files. For instance:

findViewById(R.id.customerName).setText(customer.getName())

与布局:

<TextView android:text="TextView" android:id="@id/customerName"
    android:layout_height="wrap_content" android:layout_width="fill_parent" />

现在我意识到,这可能是更好的使用 android.R 代替。

Now I realize, it might be better to use android.R instead.

findViewById(android.R.id.text1).setText(customer.getName())

与布局:

<TextView android:text="TextView" android:id="@android:id/text1"
    android:layout_height="wrap_content" android:layout_width="fill_parent" />

你遵循哪些做法?各有什么优点和缺点?

What practice do you follow? What are the advantages and disadvantages of each?

推荐答案

还有就是框架ID的 VS的没有什么大的优势或劣势的布局中自定义的ID。

There is no big advantage or disadvantage of the framework id vs the custom id in layouts.

使用框架标识符的优点:

Advantages of using framework identifiers:

  • 避免产生更多的标识符。保存的记忆一点点。
  • 必须在某些情况下使用,如 ListActivity

使用框架标识符的缺点:

Drawbacks of using framework identifiers:

  • 请不要提供一个描述性的名称 (自定义identiders不提供更多的担保)
  • <打击>请问刹车code在未来(我相信这样的事情发生,这将刚刚突破的所有的)
  • Don't provide a descriptive name (custom identiders don't offer more guarantee)
  • Will brake code in the future (I can believe this to happen, this would just break everything)

在这两种做法

  • 的code将努力在未来
  • 在缺少引用被发现在运行时,和隐藏在编译时

我想在SDK中的样本将有助于我作出决定,并且(猜猜是什么?)没有。在记事本和LunarLander应用程序使用 android.R.id 的视图标识符,而ApiDemos项目不。

I thought the the samples in the SDK would help me take a decision, and (guess what?) it doesn't. The Notepad and LunarLander applications use the android.R.id for the view identifiers, whereas the ApiDemos project doesn't.

有关GestureBuilder最佳实践它混合了这两种方法? ( @ + ID / Add按钮 @android:ID /空

Best practice for GestureBuilder which mixes both approaches? (@+id/addButton and @android:id/empty)

IHMO,为HelloActivity和喷气小子它定义 @ + ID /文<​​/ code> @ + ID / Button01 更糟糕的做法。 ..这不是描述性的,并可能已被替换( @andoid:ID /按钮1 ,或通过 @ + ID / startButton

IHMO, worse practice for HelloActivity and JetBoy which define @+id/text @+id/Button01... This is not descriptive and could have been replaced by (@andoid:id/button1, or by @+id/startButton)

这篇关于什么是更好的android.R或定制R'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆