eclipse Android:资源字符串自动生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12219299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: resource String automatic generation
提问by Tanvir
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?
我是安卓新手。当我在图形布局中添加按钮/视图时,它会以这种方式添加标签文本 - android:text="Button" 。为什么不添加"android:text="@string/my_label",并在string.xml文件中添加一个字符串资源,在eclipse中不能自动完成吗?
回答by Siddiq Abu Bakkar
I have searched a lot but I have not get any automated way to add a string to the resource file But This will save your time a lot IMHO.
我已经搜索了很多,但我还没有得到任何将字符串添加到资源文件的自动方法,但是恕我直言,这将节省您很多时间。
Select a String, click Refactor --> Android --> Extract Android String.
选择一个字符串,单击重构 --> Android --> 提取 Android 字符串。
回答by RuedigerKausK
Thanks to Brent Hronik. CTRL-1 on Windows works fine.
感谢布伦特 Hronik。Windows 上的 CTRL-1 工作正常。
回答by AxGryndr
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.
因为您不必使用@string 资源。@strings 资源的目的是使更改有关代码的元素变得更加容易。例如,如果您在多个地方使用您的应用程序标题,假设在每个对话框中,那么如果您更改标题,则必须在显示应用程序标题的所有实例中更改它。因此,在这种情况下,@string/App_Title 可以设置为“我的程序”,并且所有对话框都可以引用它。如果您将标题更改为“Hello World”,则所有这些都会更改。@strings 资源,虽然 eclipse 会尝试,但不必用于每个字符串。不使用它等同于对值进行硬编码。有很多理由支持和反对对所有事情使用 @string。
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.
不知道eclipse里有没有设置,在添加控件的时候会自动给资源文件添加一个字符串。
(EDIT: Based on other users CTRL+1 is the short cut to do this.)
(编辑:基于其他用户 CTRL+1 是执行此操作的捷径。)
回答by rolgalan
Thanks Siddiq Abu Bakkar! I didn't think it would be there.
感谢 Siddiq Abu Bakkar!我没想到它会在那里。
On Eclipse (and Windows) the shortcut is:
在 Eclipse(和 Windows)上,快捷方式是:
Alt+Shift+A(release all and then press)S
Alt+Shift+A (释放所有然后按下)S
When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.
当您第一次使用 Eclipse 时,很难理解如何使用这些“复杂”的快捷方式。
回答by Brent Hronik
You can add the string to the strings.xml by clicking command and 1(on a mac, assume it would be control 1 on a Windows or Linux box) simultaneously. This will add the resource to strings.xml and then open that up in the editor.
您可以通过同时单击 command 和 1(在 Mac 上,假设它是 Windows 或 Linux 机器上的 control 1)将字符串添加到 strings.xml。这会将资源添加到 strings.xml,然后在编辑器中打开它。
回答by Giova
I can't vote and i can't comment answers yet (missing reputation as i'm a new user)
我不能投票,我还不能评论答案(因为我是新用户,所以缺少声誉)
But i confirm :
但我确认:
1) hard type the string in your code like
mydlg.setTitle("hello guys");
2) select your string (e.g : "hello guys")
3) press Alt + Shift + A then press S
a dialog will appear to let you add a new string into resources. Everything should be already filled into that dialog box.
将出现一个对话框,让您将新字符串添加到资源中。一切都应该已经填入该对话框中。