[英]how to call Strings in mainactivity.xml from Strings.xml file
我想在 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 布局中,您可以使用text
字符串中的@string/resourceName
語法來引用字符串資源:
android:text="@string/resourceName"
使用示例中的Std_Name
字符串資源,看起來像這樣:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Std_Name" />
字符串資源的官方文檔可以在這里找到。 您還可以在應用程序資源概述的從 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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.