Printing a variables by using XML
How can I print the string variables by using XML? Here is an example shows what I want to do. I want to use the second way with string variables.
1-
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
2-
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, Android! I am a stri开发者_C百科ng resource!</string>
<string name="app_name">Hello, Android</string>
</resources>
Java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
OK, first your main layout must contain a TextView with an id specified. Then you can use this code:
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.text_view);
textView.setText(getString(R.id.hello));
Hope this helps.
精彩评论