Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Building Your First App.doc
Скачиваний:
0
Добавлен:
01.04.2025
Размер:
484 Кб
Скачать
☆

Add String Resources

When you need to add text in the user interface, you should always specify each string as a resource. String resources allow you to manage all UI text in a single location, which makes it easier to find and update text. Externalizing the strings also allows you to localize your app to different languages by providing alternative definitions for each string resource.

By default, your Android project includes a string resource file at res/values/strings.xml. Add a new string named "edit_message" and set the value to "Enter a message." (You can delete the "hello_world" string.)

While you’re in this file, also add a "Send" string for the button you’ll soon add, called "button_send".

The result for strings.xml looks like this:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string name="app_name">My First App</string>     <string name="edit_message">Enter a message</string>     <string name="button_send">Send</string>     <string name="menu_settings">Settings</string>     <string name="title_activity_main">MainActivity</string> </resources>

For more information about using string resources to localize your app for other languages, see the Supporting Different Devices class.

Add a Button

Now add a <Button> to the layout, immediately following the <EditText> element:

    <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/button_send" />

The height and width are set to "wrap_content" so the button is only as big as necessary to fit the button's text. This button doesn't need the android:id attribute, because it won't be referenced from the activity code.

Make the Input Box Fill in the Screen Width

The layout is currently designed so that both the EditText and Button widgets are only as big as necessary to fit their content, as shown in figure 2.

Figure 2. The EditText and Button widgets have their widths set to "wrap_content".

This works fine for the button, but not as well for the text field, because the user might type something longer. So, it would be nice to fill the unused screen width with the text field. You can do this inside a LinearLayout with the weight property, which you can specify using the android:layout_weight attribute.

The weight value is a number that specifies the amount of remaining space each view should consume, relative to the amount consumed by sibling views. This works kind of like the amount of ingredients in a drink recipe: "2 parts vodka, 1 part coffee liqueur" means two-thirds of the drink is vodka. For example, if you give one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view fills 2/3 of the remaining space and the second view fills the rest. If you add a third view and give it a weight of 1, then the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining two each get 1/4.

The default weight for all views is 0, so if you specify any weight value greater than 0 to only one view, then that view fills whatever space remains after all views are given the space they require. So, to fill the remaining space in your layout with the EditText element, give it a weight of 1 and leave the button with no weight.

    <EditText         android:layout_weight="1"         ... />

In order to improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). Setting the width to zero improves layout performance because using "wrap_content" as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.

    <EditText         android:layout_weight="1"         android:layout_width="0dp"         ... />

Figure 3 shows the result when you assign all weight to the EditText element.

Figure 3. The EditText widget is given all the layout weight, so fills the remaining space in the LinearLayout.

Here’s how your complete layout file should now look:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal">     <EditText android:id="@+id/edit_message"         android:layout_weight="1"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:hint="@string/edit_message" />     <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/button_send" /> </LinearLayout>

This layout is applied by the default Activity class that the SDK tools generated when you created the project, so you can now run the app to see the results:

  • In Eclipse, click Run from the toolbar.

  • Or from a command line, change directories to the root of your Android project and execute:

ant debug adb install bin/MyFirstApp-debug.apk

Continue to the next lesson to learn how you can respond to button presses, read content from the text field, start another activity, and more.

Previ

Starting Another Activity

Previous Next

This lesson teaches you to

  1. Respond to the Send Button

  2. Build an Intent

  3. Start the Second Activity

  4. Create the Second Activity

  5. Receive the Intent

  6. Display the Message

You should also read

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]