Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use string Resources in android Development

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use string resources in android development". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use string resources in android development.

In an Android project, we may use a large number of strings as prompts. These strings can be declared in the configuration file as string resources, thus achieving the configurability of the program. In the code, we use the Context.getString () method to get the string by passing the resource ID parameter, or we can reference the string resource in other resource files in the format: "@ string/ string resource name.

1. Definition of string resource XML file

We use tables to illustrate how string resources are defined, including the location of the resource, the format of the XML file, how to get the resource, and how to reference the resource.

two。 The use of string resource XML files

The following is an example to demonstrate the use of resource files. Two string resources are used in this example: one is referenced in the layout file and the other is referenced in the Java code. The steps of the example are described below. Under the resvalues directory of the project, create a string resource file stirngs.xml, as shown below:

Test Resources

Reference from the code!

Reference from the resource file!

In the reslayout directory of the project, define a layout file test_string.xml. Add two TextView view objects to the layout file: the text content of the first TextView refers directly to the resources in the strings.xml file, and the text content of the second TextView is set in the code.

Android:orientation= "vertical"

Android:layout_width= "fill_parent"

Android:layout_height= "fill_parent" >

Android:text= "@ string/test_str1"

Android:id= "@ + id/myTextView01"

Android:layout_width= "wrap_content"

Android:layout_height= "wrap_content" / >

Android:text= ""

Android:id= "@ + id/myTextView02"

Android:layout_width= "wrap_content"

Android:layout_height= "wrap_content" / >

In the com.amaker.ch03.string package of the project, create a TestStringActivity class. In the onCreate () method of this class, set the current view layout and get an instance of TextView. Through the Context.getString () method, you get the string constant from the string resource and set it to the text content of the TextView.

Package com.amaker.ch03.string

Import android.app.Activity

Import android.os.Bundle

Import android.widget.TextView

Import com.amaker.test.R

/ * *

* Test string resources

, /

Public class TestStringActivity extends Activity {

Private TextView myTextView

@ Override

Public void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

SetContentView (R.layout.test_string)

MyTextView = (TextView) findViewById (R.id.myTextView02)

String str = getString (R.string.test_str2). ToString ()

MyTextView.setText (str)

}

}

At this point, I believe you have a deeper understanding of "how to use string resources in android development". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report