Android:资源字符串自动生成
我是 Android 新手.当我在图形布局中添加按钮/视图时,它会以这种方式添加标签文本 - android:text="Button" .为什么不加"android:text="@string/my_label",在string.xml文件里加个字符串资源,eclipse中不能自动完成吗?
I'm new to Android. When I add a button/views in Graphical layout it adds the label text this way- android:text="Button" . Why doesnt it add "android:text="@string/my_label" and add a string resource in string.xml file. Can't it be done automatically in eclipse?
推荐答案
因为您不必使用@string 资源.@strings 资源的目的是使更改代码元素变得更加容易.例如,如果您在多个地方使用您的应用程序标题,假设在每个对话框中,那么如果您更改标题,则必须在显示应用程序标题的所有实例中更改它.因此在这种情况下,@string/App_Title 可以设置为我的程序",并且所有对话框都可以引用它.如果您将标题更改为Hello World",那么所有这些都会更改.@strings 资源,虽然 eclipse 尝试,但不必用于每个字符串.不使用它相当于对值进行硬编码.有很多理由支持和反对使用@string.
Because you don't have to use the @string resource. The purpose of the @strings resource is to make it easier to change elements about your code. For example, if you are using your application title in mutliple places, let's say in every dialog box, then if you change the title you would have to change it in all the instances that the app title is being display. So in this instance the @string/App_Title could be set to "My Program" and all of the dialog boxes can reference that. If you change the title to "Hello World" then all of these are changed. The @strings resource, while eclipse tries, doesn't have to be used for every string. Not using it is the equivalent to hard coding the value. There are plenty of reasons for and against using @string for everything.
不知道eclipse中是否有设置在添加控件时会自动将字符串添加到资源文件中.
I am not sure if there is a setting in eclipse that will automatically add a string to the resource file when the control is added.
(基于其他用户,CTRL+1 是执行此操作的捷径.)
( Based on other users CTRL+1 is the short cut to do this.)
相关文章