1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Androi part 29 pptx

8 228 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

197 197 Chapter Working with Resources Resources are static bits of information held outside the Java source code. You have seen one type of resource—the layout—frequently in the examples in this book. As you’ll learn in this chapter, there are many other types of resources, such as images and strings, that you can take advantage of in your Android applications. The Resource Lineup Resources are stored as files under the res/ directory in your Android project layout. With the exception of raw resources (res/raw/), all the other types of resources are parsed for you, either by the Android packaging system or by the Android system on the device or emulator. So, for example, when you lay out an activity’s UI via a layout resource (res/layout/), you do not need to parse the layout XML yourself; Android handles that for you. In addition to layout resources (introduced in Chapter 4) and animation resources (introduced in Chapter 9), several other types of resources are available, including the following:  Images (res/drawable/), for putting static icons or other pictures in a user interface  Raw (res/raw/), for arbitrary files that have meaning to your application but not necessarily to Android frameworks  Strings, colors, arrays, and dimensions (res/values/), to both give these sorts of constants symbolic names and to keep them separate from the rest of the code (e.g., for internationalization and localization)  XML (res/xml/), for static XML files containing your own data and structure 20 CHAPTER 20: Working with Resources 198 String Theory Keeping your labels and other bits of text outside the main source code of your application is generally considered to be a very good idea. In particular, it helps with internationalization and localization, covered in the “Different Strokes for Different Folks” section later in this chapter. Even if you are not going to translate your strings to other languages, it is easier to make corrections if all the strings are in one spot, instead of scattered throughout your source code. Android supports regular externalized strings, along with string formats, where the string has placeholders for dynamically inserted information. On top of that, Android supports simple text formatting, called styled text, so you can make your words be bold or italic intermingled with normal text. Plain Strings Generally speaking, all you need for plain strings is an XML file in the res/values directory (typically named res/values/strings.xml), with a resources root element, and one child string element for each string you wish to encode as a resource. The string element takes a name attribute, which is the unique name for this string, and a single text element containing the text of the string. <resources> <string name="quick">The quick brown fox </string> <string name="laughs">He who laughs last </string> </resources> The only tricky part is if the string value contains a quotation mark (") or an apostrophe ('). In those cases, you will want to escape those values, by preceding them with a backslash (e.g., These are the times that try men\'s souls). Or, if it is just an apostrophe, you could enclose the value in quotation marks (e.g., "These are the times that try men's souls."). You can then reference this string from a layout file (as @string/ , where the ellipsis is the unique name, such as @string/laughs). Or you can get the string from your Java code by calling getString() with the resource ID of the string resource, which is the unique name prefixed with R.string. (e.g., getString(R.string.quick)). String Formats As with other implementations of the Java language, Android’s Dalvik virtual machine supports string formats. Here, the string contains placeholders representing data to be replaced at runtime by variable information (e.g., My name is %1$s). Plain strings stored as resources can be used as string formats: String strFormat=getString(R.string.my_name); String strResult=String.format(strFormat, "Tim"); ((TextView)findViewById(R.id.some_label)).setText(strResult); CHAPTER 20: Working with Resources 199 Styled Text If you want really rich text, you should have raw resources containing HTML, and then pour those into a WebKit widget. However, for light HTML formatting, using <b>, <i>, and <u>, you can just use a string resource. The catch is that you must escape the HTML tags, rather than treating them normally: <resources> <string name="b">This has &lt;b&gt;bold&lt;/b&gt; in it.</string> <string name="i">Whereas this has &lt;i&gt;italics&lt;/i&gt;!</string> </resources> You can access these the same way as you get plain strings, with the exception that the result of the getString() call is really an object supporting the android.text.Spanned interface: ((TextView)findViewById(R.id.another_label)) .setText(getString(R.string.b)); Styled String Formats Where styled text gets tricky is with styled string formats, as String.format() works on String objects, not Spanned objects with formatting instructions. If you really want to have styled string formats, here is the work-around: 1. Entity-escape the angle brackets in the string resource (e.g., this is &lt;b&gt;%1$s&lt;/b&gt;). 2. Retrieve the string resource as normal, though it will not be styled at this point (e.g., getString(R.string.funky_format)). 3. Generate the format results, being sure to escape any string values you substitute, in case they contain angle brackets or ampersands. String.format(getString(R.string.funky_format), TextUtils.htmlEncode(strName)); 4. Convert the entity-escaped HTML into a Spanned object via Html.fromHtml(). someTextView.setText(Html .fromHtml(resultFromStringFormat)); To see this in action, let’s look at the Resources/Strings demo. Here is the layout file: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" CHAPTER 20: Working with Resources 200 android:layout_height="wrap_content" > <Button android:id="@+id/format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn_name" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> As you can see, it is just a button, a field, and a label. The idea is for users to enter their name in the field, and then click the button to cause the label to be updated with a formatted message containing their name. The Button in the layout file references a string resource (@string/btn_name), so we need a string resource file (res/values/strings.xml): <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">StringsDemo</string> <string name="btn_name">Name:</string> <string name="funky_format">My name is &lt;b&gt;%1$s&lt;/b&gt;</string> </resources> The app_name resource is automatically created by the activityCreator script. The btn_name string is the caption of the Button, while our styled string format is in funky_format. Finally, to hook all this together, we need a pinch of Java: package com.commonsware.android.strings; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.text.Html; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class StringsDemo extends Activity { EditText name; TextView result; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); CHAPTER 20: Working with Resources 201 name=(EditText)findViewById(R.id.name); result=(TextView)findViewById(R.id.result); Button btn=(Button)findViewById(R.id.format); btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { applyFormat(); } }); } private void applyFormat() { String format=getString(R.string.funky_format); String simpleResult=String.format(format, TextUtils.htmlEncode(name.getText().toString())); result.setText(Html.fromHtml(simpleResult)); } } The string resource manipulation can be found in applyFormat(), which is called when the button is clicked. First, we get our format via getString() (something we could have done at onCreate() time for efficiency). Next, we format the value in the field using this format, getting a String back, since the string resource is in entity-encoded HTML. Note the use of TextUtils.htmlEncode() to entity-encode the entered name, in case someone decides to use an ampersand or something. Finally, we convert the simple HTML into a styled text object via Html.fromHtml() and update our label. When the activity is first launched, we have an empty label, as shown in Figure 20–1. Figure 20–1. The StringsDemo sample application, as initially launched CHAPTER 20: Working with Resources 202 When you fill in a name and click the button, you get the result shown in Figure 20–2. Figure 20–2. The same application, after filling in some heroic figure's name Got the Picture? Android supports images in the PNG, JPEG, and GIF formats. GIF is officially discouraged, however. PNG is the overall preferred format. Images can be used anywhere you require a Drawable, such as the image and background of an ImageView. Using images is simply a matter of putting your image files in res/drawable/ and then referencing them as a resource. Within layout files, images are referenced as @drawable/ where the ellipsis is the base name of the file (e.g., for res/drawable/foo.png, the resource name is @drawable/foo). In Java, where you need an image resource ID, use R.drawable. plus the base name (e.g., R.drawable.foo). To demonstrate, let’s update the previous example to use an icon for the button instead of the string resource. This can be found as Resources/Images. First, we slightly adjust the layout file, using an ImageButton and referencing a Drawable named @drawable/icon: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageButton android:id="@+id/format" CHAPTER 20: Working with Resources 203 android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> Next, we need to put an image file in res/drawable with a base name of icon. In this case, we use a 32-by-32 PNG file from the Nuvola icon set (http://www.icon- king.com/projects/nuvola/). Finally, we twiddle the Java source, replacing our Button with an ImageButton: package com.commonsware.android.images; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.text.Html; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.EditText; import android.widget.TextView; public class ImagesDemo extends Activity { EditText name; TextView result; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); name=(EditText)findViewById(R.id.name); result=(TextView)findViewById(R.id.result); ImageButton btn=(ImageButton)findViewById(R.id.format); btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { applyFormat(); } }); } private void applyFormat() { String format=getString(R.string.funky_format); String simpleResult=String.format(format, CHAPTER 20: Working with Resources 204 TextUtils.htmlEncode(name.getText().toString())); result.setText(Html.fromHtml(simpleResult)); } } Now, our button has the desired icon, as shown in Figure 20–3. Figure 20–3. The ImagesDemo sample application XML: The Resource Way If you wish to package static XML with your application, you can use an XML resource. Simply put the XML file in res/xml/. Then you can access it by getXml() on a Resources object, supplying it a resource ID of R.xml. plus the base name of your XML file. For example, in an activity, with an XML file of words.xml, you could call getResources().getXml(R.xml.words). This returns an instance of an XmlPullParser, found in the org.xmlpull.v1 Java namespace. An XML pull parser is event-driven: you keep calling next() on the parser to get the next event, which could be START_TAG, END_TAG, END_DOCUMENT, and so on. On a START_TAG event, you can access the tag’s name and attributes; a single TEXT event represents the concatenation of all text nodes that are direct children of this element. By looping, testing, and invoking per-element logic, you parse the file. To see this in action, let’s rewrite the Java code for the Files/Static sample project to use an XML resource. This new project, Resources/XML, requires that you place the words.xml file from Static not in res/raw/, but in res/xml/. The layout stays the same, so all that needs to be replaced is the Java source: . com.commonsware.android.strings; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.text.Html; import android.view.View; import android.widget.Button;. com.commonsware.android.images; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.text.Html; import android.view.View; import android.widget.Button;. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"

Ngày đăng: 01/07/2014, 21:20

Xem thêm: Lập trình Androi part 29 pptx

Mục lục

    Contents at a Glance

    Challenges of Smartphone Programming

    What Androids Are Made Of

    Stuff at Your Disposal

    The Sweat Off Your Brow

    And Now, the Rest of the Story

    What You Get Out of It

    In the Beginning, There Was the Root, And It Was Good

    Permissions, Instrumentations, and Applications (Oh My!)

    Your Application Does Something, Right?

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN