Android: resource String automatic generation

前端 未结 6 1120
心在旅途
心在旅途 2021-01-20 06:52

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_l

相关标签:
6条回答
  • 2021-01-20 07:12

    Thanks to Brent Hronik. CTRL-1 on Windows works fine.

    0 讨论(0)
  • 2021-01-20 07:24

    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.

    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.

    (EDIT: Based on other users CTRL+1 is the short cut to do this.)

    0 讨论(0)
  • 2021-01-20 07:28

    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.

    0 讨论(0)
  • 2021-01-20 07:29

    Thanks Siddiq Abu Bakkar! I didn't think it would be there.

    On Eclipse (and Windows) the shortcut is:

    Alt+Shift+A (release all and then press) S

    When you use Eclipse for first time it's not easy understand how to use these kind of "complex" shortcuts.

    0 讨论(0)
  • 2021-01-20 07:33

    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.

    0 讨论(0)
  • 2021-01-20 07:34

    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.

    0 讨论(0)
提交回复
热议问题