-
我想在TextView中调用它
<string name="Std_Name">Name: Muhammad Abdullah</string> <string name="Std_Phone">Phone: +923338157028</string>
如果要将值从strings.xml添加到mainactivity.xml
中的TextView
,请在xml
android:text="@string/Std_Name"
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5sp"
android:lineSpacingExtra="2sp"
android:text="@string/Std_Name" // Add this line to your textView
android:textColor="@color/yellow_dark"
android:textSize="@dimen/activity_text_size_small"
android:textStyle="normal" />
或者,如果要将strings.xml
的文本添加到MainActivity.java
的textview,请添加以下代码
TextView textView = findViewById(R.id.textView1);
textView.setText(getResources().getString(R.string.Std_Name));
从内部XML布局,您可以使用@string/resourceName
text
字符串内的语法来引用一个字符串资源:
android:text="@string/resourceName"
使用示例中的Std_Name
字符串资源,它看起来像这样:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Std_Name" />
关于字符串资源的官方文档可以在这里找到。您还可以在App资源概述的"从XML访问资源"一节中找到一个很好的示例。
试试这个
String myName = getResources().getString(R.string.Std_Name);
String myPhone = getResources().getString(R.string.Std_Phone);
// Then do whatever you want with these values
调用Textview文本字段
android:text="@string/Std_Name"