Android Training/Building a Simple User Interface: Unterschied zwischen den Versionen

Aus Android Wiki
(Diese Seite wurde zum Übersetzen freigegeben)
Keine Bearbeitungszusammenfassung
 
(14 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
{{DISPLAYTITLE:Android Training: Building a Simple User Interface}}
<languages/>
<languages/>
[[Datei:Viewgroup.png|thumb|Illustration of how ViewGroup objects form branches in the layout and contain View objects.]]
[[Datei:Viewgroup.png|thumb|<translate><!--T:37-->
Illustration of how ViewGroup objects form branches in the layout and contain View objects.</translate>]]
<translate><!--T:1-->
<translate><!--T:1-->
In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds when the button is pressed by sending the content of the text field to another activity.
In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds when the button is pressed by sending the content of the text field to another activity.


<!--T:2-->
<!--T:2-->
The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects. View objects are usually UI widgets such as buttons or text fields. ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.
The graphical user interface for an Android app is built using a hierarchy of <code>View</code> and <code>ViewGroup</code> objects. <code>View</code> objects are usually UI widgets such as buttons or text fields. <code>ViewGroup</code> objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.


<!--T:3-->
<!--T:3-->
Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.
Android provides an XML vocabulary that corresponds to the subclasses of <code>View</code> and <code>ViewGroup</code> so you can define your UI in XML using a hierarchy of UI elements.


<!--T:4-->
<!--T:4-->
Layouts are subclasses of the ViewGroup. In this exercise, you'll work with a LinearLayout.
<code>Layouts</code> are subclasses of the <code>ViewGroup</code>. In this exercise, you'll work with a <code>LinearLayout</code>.


== Create a Linear Layout == <!--T:5-->
== Create a Linear Layout == <!--T:5-->
Zeile 48: Zeile 48:
<!--T:10-->
<!--T:10-->
For more information about layout properties, see the Layout guide.
For more information about layout properties, see the Layout guide.
== Add a Text Field == <!--T:11-->
As with every [http://developer.android.com/reference/android/view/View.html <code>View</code>] object, you must define certain XML attributes to specify the [http://developer.android.com/reference/android/widget/EditText.html <code>EditText</code>] object's properties.
<!--T:12-->
# In the <code>activity_my.xml</code> file, within the <code><LinearLayout></code> element, define an <code><EditText></code> element with the id attribute set to @+id/edit_message.
# Define the <code>layout_width</code> and <code>layout_height</code> attributes as <code>wrap_content</code>.
# Define a <code>hint</code> attribute as a string object named <code>edit_message</code>.
<!--T:13-->
The <code><EditText></code> element should read as follows:
</translate>
''res/layout/activity_my.xml''
<syntaxhighlight lang="xml">
<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
</syntaxhighlight>
<translate>
<!--T:14-->
Here are the <EditText> attributes you added:
=== [http://developer.android.com/reference/android/view/View.html#attr_android:id <code>android:id</code>] === <!--T:15-->
This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you'll see this in the next lesson).
The at sign (<code>@</code>) is required when you're referring to any resource object from XML. It is followed by the resource type (<code>id</code> in this case), a slash, then the resource name (<code>edit_message</code>).
The plus sign (<code>+</code>) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's <code>gen/R.java</code> file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.
=== [http://developer.android.com/reference/android/view/View.html#attr_android:layout_width <code>android:layout_width</code>] and [http://developer.android.com/reference/android/view/View.html#attr_android:layout_height <code>android:layout_height</code>] === <!--T:16-->
Instead of using specific sizes for the width and height, the <code>"wrap_content"</code> value specifies that the view should be only as big as needed to fit the contents of the view. If you were to instead use <code>"match_parent"</code>, then the <code>EditText</code> element would fill the screen, because it would match the size of the parent <code>LinearLayout</code>. For more information, see the Layouts guide.
=== [http://developer.android.com/reference/android/widget/TextView.html#attr_android:hint <code>android:hint</code>] === <!--T:17-->
This is a default string to display when the text field is empty. Instead of using a hard-coded string as the value, the <code>"@string/edit_message"</code> value refers to a string resource defined in a separate file. Because this refers to a concrete resource (not just an identifier), it does not need the plus sign. However, because you haven't defined the string resource yet, you’ll see a compiler error at first. You'll fix this in the next section by defining the string.
== Add String Resources == <!--T:18-->
By default, your Android project includes a string resource file at <code>res/values/strings.xml</code>. Here, you'll add a new string named <code>"edit_message"</code> and set the value to "Enter a message."
<!--T:19-->
# In [[Android Studio]], from the <code>res/values</code> directory, open <code>strings.xml</code>.
# Add a line for a string named <code>"edit_message"</code> with the value, "Enter a message".
# Add a line for a string named <code>"button_send"</code> with the value, "Send".
# You'll create the button that uses this string in the next section.
# Remove the line for the <code>"hello world"</code> string.
<!--T:20-->
The result for <code>strings.xml</code> looks like this:
</translate>
''res/values/strings.xml''
<syntaxhighlight lang="xml">
<?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="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>
</syntaxhighlight>
<translate>
<!--T:21-->
For text in the user interface, always specify each string as a resource. String resources allow you to manage all UI text in a single location, which makes the text easier to find and update. Externalizing the strings also allows you to localize your app to different languages by providing alternative definitions for each string resource.
<!--T:22-->
For more information about using string resources to localize your app for other languages, see the Supporting Different Devices class.
== Add a Button == <!--T:23-->
# In Android Studio, from the <code>res/layout</code> directory, edit the <code>activity_my.xml</code> file.
# Within the <code><LinearLayout></code> element, define a [http://developer.android.com/reference/android/widget/Button.html <code><Button></code>] element immediately following the <code><EditText></code> element.
# Set the button's width and height attributes to <code>"wrap_content"</code> so the button is only as big as necessary to fit the button's text label.
# Define the button's text label with the [http://developer.android.com/reference/android/widget/TextView.html#attr_android:text <code>android:text</code>] attribute; set its value to the <code>button_send</code> string resource you defined in the previous section.
Your <code><LinearLayout></code> should look like this:
</translate>
''res/layout/activity_my.xml''
<syntaxhighlight lang="xml">
<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_width="wrap_content"
        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>
</syntaxhighlight>
<translate>
<!--T:24-->
'''Note''': This button doesn't need the <code>android:id</code> attribute, because it won't be referenced from the activity code.
</translate>
[[Datei:edittext_wrap.png|thumb|<translate><!--T:25-->
The EditText and Button widgets have their widths set to "wrap_content".</translate>]]
<translate>
<!--T:26-->
The layout is currently designed so that both the <code>EditText</code> and <code>Button</code> widgets are only as big as necessary to fit their content, as shown in the picture.
<!--T:27-->
This works fine for the button, but not as well for the text field, because the user might type something longer. It would be nice to fill the unused screen width with the text field. You can do this inside a <code>LinearLayout</code> with the weight property, which you can specify using the [http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight <code>android:layout_weight</code>] attribute.
<!--T:28-->
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 soda, 1 part syrup" means two-thirds of the drink is soda. 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.
<!--T:29-->
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.
== Make the Input Box Fill in the Screen Width == <!--T:30-->
To fill the remaining space in your layout with the <code>EditText</code> element, do the following:
<!--T:31-->
# In the <code>activity_my.xml</code> file, assign the <code><EditText></code> element's <code>layout_weight</code> attribute a value of <code>1</code>.
# Also, assign <code><EditText></code> element's <code>layout_width</code> attribute a value of <code>0dp</code>.
</translate>
#: ''res/layout/activity_my.xml''
#: <syntaxhighlight lang="xml">
<EditText
    android:layout_weight="1"
    android:layout_width="0dp"
    ... />
</syntaxhighlight>
<translate>
<!--T:32-->
To improve the layout efficiency when you specify the weight, you should change the width of the <code>EditText</code> to be zero (0dp). Setting the width to zero improves layout performance because using <code>"wrap_content"</code> 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.
<!--T:33-->
Here’s how your complete <code>activity_my.xml</code> layout file should now look:
</translate>
''res/layout/activity_my.xml''
<syntaxhighlight lang="xml">
<?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>
</syntaxhighlight>
<translate>
== Run Your App == <!--T:34-->
This layout is applied by the default <code>Activity</code> class that the SDK tools generated when you created the project. Run the app to see the results:
<!--T:35-->
# In Android Studio, from the toolbar, click '''Run'''.
# Or from a command line, change directories to the root of your Android project and execute:</translate>
#: <code>ant debug</code>
#: <code>adb install bin/MyFirstApp-debug.apk</code>
<translate>
<!--T:36-->
Continue to the next lesson to learn how to respond to button presses, read content from the text field, start another activity, and more.
</translate>
</translate>


{{Android Training/ Vorlage:Attribution}}
{{TNT|Android Training/Attribution}}

Aktuelle Version vom 18. Dezember 2015, 18:23 Uhr

Sprachen:
Illustration of how ViewGroup objects form branches in the layout and contain View objects.

In this lesson, you create a layout in XML that includes a text field and a button. In the next lesson, your app responds when the button is pressed by sending the content of the text field to another activity.

The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects. View objects are usually UI widgets such as buttons or text fields. ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.

Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.

Layouts are subclasses of the ViewGroup. In this exercise, you'll work with a LinearLayout.

Create a Linear Layout[Bearbeiten | Quelltext bearbeiten]

  1. In Android Studio, from the res/layout directory, open the activity_my.xml file.
    The BlankActivity template you chose when you created this project includes the activity_my.xml file with a RelativeLayout root view and a TextView child view.
  2. In the Preview pane, click the Hide icon to close the Preview pane.
  3. In Android Studio, when you open a layout file, you’re first shown the Preview pane. Clicking elements in this pane opens the WYSIWYG tools in the Design pane. For this lesson, you’re going to work directly with the XML.
  4. Delete the <TextView> element.
  5. Change the <RelativeLayout> element to <LinearLayout>.
  6. Add the android:orientation attribute and set it to "horizontal".
  7. Remove the android:padding attributes and the tools:context attribute.

The result looks like this: res/layout/activity_my.xml

<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" >
</LinearLayout>

LinearLayout is a view group (a subclass of ViewGroup) that lays out child views in either a vertical or horizontal orientation, as specified by the android:orientation attribute. Each child of a LinearLayout appears on the screen in the order in which it appears in the XML.

Two other attributes, android:layout_width and android:layout_height, are required for all views in order to specify their size.

Because the LinearLayout is the root view in the layout, it should fill the entire screen area that's available to the app by setting the width and height to "match_parent". This value declares that the view should expand its width or height to match the width or height of the parent view.

For more information about layout properties, see the Layout guide.

Add a Text Field[Bearbeiten | Quelltext bearbeiten]

As with every View object, you must define certain XML attributes to specify the EditText object's properties.

  1. In the activity_my.xml file, within the <LinearLayout> element, define an <EditText> element with the id attribute set to @+id/edit_message.
  2. Define the layout_width and layout_height attributes as wrap_content.
  3. Define a hint attribute as a string object named edit_message.

The <EditText> element should read as follows: res/layout/activity_my.xml

<EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

Here are the <EditText> attributes you added:

android:id[Bearbeiten | Quelltext bearbeiten]

This provides a unique identifier for the view, which you can use to reference the object from your app code, such as to read and manipulate the object (you'll see this in the next lesson). The at sign (@) is required when you're referring to any resource object from XML. It is followed by the resource type (id in this case), a slash, then the resource name (edit_message). The plus sign (+) before the resource type is needed only when you're defining a resource ID for the first time. When you compile the app, the SDK tools use the ID name to create a new resource ID in your project's gen/R.java file that refers to the EditText element. With the resource ID declared once this way, other references to the ID do not need the plus sign. Using the plus sign is necessary only when specifying a new resource ID and not needed for concrete resources such as strings or layouts. See the sidebox for more information about resource objects.

android:layout_width and android:layout_height[Bearbeiten | Quelltext bearbeiten]

Instead of using specific sizes for the width and height, the "wrap_content" value specifies that the view should be only as big as needed to fit the contents of the view. If you were to instead use "match_parent", then the EditText element would fill the screen, because it would match the size of the parent LinearLayout. For more information, see the Layouts guide.

android:hint[Bearbeiten | Quelltext bearbeiten]

This is a default string to display when the text field is empty. Instead of using a hard-coded string as the value, the "@string/edit_message" value refers to a string resource defined in a separate file. Because this refers to a concrete resource (not just an identifier), it does not need the plus sign. However, because you haven't defined the string resource yet, you’ll see a compiler error at first. You'll fix this in the next section by defining the string.

Add String Resources[Bearbeiten | Quelltext bearbeiten]

By default, your Android project includes a string resource file at res/values/strings.xml. Here, you'll add a new string named "edit_message" and set the value to "Enter a message."

  1. In Android Studio, from the res/values directory, open strings.xml.
  2. Add a line for a string named "edit_message" with the value, "Enter a message".
  3. Add a line for a string named "button_send" with the value, "Send".
  4. You'll create the button that uses this string in the next section.
  5. Remove the line for the "hello world" string.

The result for strings.xml looks like this:

res/values/strings.xml

<?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="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>

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

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

Add a Button[Bearbeiten | Quelltext bearbeiten]

  1. In Android Studio, from the res/layout directory, edit the activity_my.xml file.
  2. Within the <LinearLayout> element, define a <Button> element immediately following the <EditText> element.
  3. Set the button's width and height attributes to "wrap_content" so the button is only as big as necessary to fit the button's text label.
  4. Define the button's text label with the android:text attribute; set its value to the button_send string resource you defined in the previous section.

Your <LinearLayout> should look like this:

res/layout/activity_my.xml

<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_width="wrap_content"
        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>

Note: This button doesn't need the android:id attribute, because it won't be referenced from the activity code.

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

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 the picture.

This works fine for the button, but not as well for the text field, because the user might type something longer. 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 soda, 1 part syrup" means two-thirds of the drink is soda. 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.

Make the Input Box Fill in the Screen Width[Bearbeiten | Quelltext bearbeiten]

To fill the remaining space in your layout with the EditText element, do the following:

  1. In the activity_my.xml file, assign the <EditText> element's layout_weight attribute a value of 1.
  2. Also, assign <EditText> element's layout_width attribute a value of 0dp.
    res/layout/activity_my.xml
    <EditText
        android:layout_weight="1"
        android:layout_width="0dp"
        ... />
    

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.

Here’s how your complete activity_my.xml layout file should now look: res/layout/activity_my.xml

<?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>

Run Your App[Bearbeiten | Quelltext bearbeiten]

This layout is applied by the default Activity class that the SDK tools generated when you created the project. Run the app to see the results:

  1. In Android Studio, from the toolbar, click Run.
  2. 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 to respond to button presses, read content from the text field, start another activity, and more.