android:text ="@ string/hello"和普通右键单击 - >文本视图组件 - > EditText之间的区别

Pra*_*abu 2 java android android-layout

当我在教程的帮助下工作时,我发现这在textview android:text - "@ string/hello"中显示了一些错误.

然后我浏览了图形视图并右键单击该组件并输入文本.然后该错误删除并通知我

**Hardcoded string hello should use String resources**
Run Code Online (Sandbox Code Playgroud)

Raj*_*hah 5

在android中,"@ string /"是指包浏览Project> res> values> location中的string.xml.

String.xml包含一个xml文件,该文件引用具有id的字符串.例如:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World</string>
    <string name="app_name">My app</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

这里name ="hello"是id&"Hello World"是它的值.使用@ string/hello时,将显示该值.

类似于"@ drawable /"的情况.它将参考使用的图像.还有很多.

您可以通过编程方式在文本视图中设置文本.例如:

TextView tv = (TextView)findViewById(R.id.text1);     //text1 is the id u provide in xml file
tv.setText("Hello World");
Run Code Online (Sandbox Code Playgroud)

我希望它对你有帮助.