Bearbeiten von „Android Training/Building a Simple User Interface

    Du bearbeitest die Quelle einer übersetzbaren Seite. Bitte stelle sicher, dass du mit der folgenden Dokumentation vertraut bist:

    Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden. Ein eigenes Benutzerkonto hat eine ganze Reihe von Vorteilen.

    Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.

    Aktuelle Version Dein Text
    Zeile 1: Zeile 1:
    <languages/>
    {{DISPLAYTITLE:Android Training: Building a Simple User Interface}}
    [[Datei:Viewgroup.png|thumb|<translate><!--T:37-->
    <languages />
    Illustration of how ViewGroup objects form branches in the layout and contain View objects.</translate>]]
    [[Datei:Viewgroup.png|thumb|Illustration of how ViewGroup objects form branches in the layout and contain View objects.]]
    <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 <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.
    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.


    <!--T:3-->
    <!--T:3-->
    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.
    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.


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


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


    {{TNT|Android Training/Attribution}}
    {{Android Training/ Vorlage:Attribution}}
    Bitte kopiere keine Webseiten, die nicht deine eigenen sind, benutze keine urheberrechtlich geschützten Werke ohne Erlaubnis des Urhebers!
    Du gibst uns hiermit deine Zusage, dass du den Text selbst verfasst hast, dass der Text Allgemeingut (public domain) ist, oder dass der Urheber seine Zustimmung gegeben hat. Falls dieser Text bereits woanders veröffentlicht wurde, weise bitte auf der Diskussionsseite darauf hin. Bitte beachte, dass alle Android Wiki-Beiträge automatisch unter der „Creative Commons Attribution/Share-Alike Lizenz 3.0“ stehen. Falls du nicht möchtest, dass deine Arbeit hier von anderen verändert und verbreitet wird, dann klicke nicht auf „Seite speichern“.

    Um das Wiki vor automatisiertem Bearbeitungsspam zu schützen, bitten wir dich, das folgende CAPTCHA zu lösen:

    Abbrechen Bearbeitungshilfe (wird in einem neuen Fenster geöffnet)

    Folgende Vorlagen werden auf dieser Seite verwendet: