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

Aus Android Wiki
(Test für Translate)
(Diese Seite wurde zum Übersetzen freigegeben)
Zeile 2: Zeile 2:
<languages/>
<languages/>
[[Datei:Viewgroup.png|thumb|Illustration of how ViewGroup objects form branches in the layout and contain View objects.]]
[[Datei:Viewgroup.png|thumb|Illustration of how ViewGroup objects form branches in the layout and contain View objects.]]
<translate>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.
<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.


<!--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 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-->
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 View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.


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


== Create a Linear Layout ==
== Create a Linear Layout == <!--T:5-->
# In Android Studio, from the <code>res/layout</code> directory, open the <code>activity_my.xml</code> file.
# In Android Studio, from the <code>res/layout</code> directory, open the <code>activity_my.xml</code> file.
#: The BlankActivity template you chose when you created this project includes the <code>activity_my.xml</code> file with a RelativeLayout root view and a <code>TextView</code> child view.
#: The BlankActivity template you chose when you created this project includes the <code>activity_my.xml</code> file with a RelativeLayout root view and a <code>TextView</code> child view.
Zeile 20: Zeile 24:
# Remove the <code>android:padding</code> attributes and the <code>tools:context</code> attribute.
# Remove the <code>android:padding</code> attributes and the <code>tools:context</code> attribute.


<!--T:6-->
The result looks like this:
The result looks like this:
</translate>
</translate>
Zeile 32: Zeile 37:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:7-->
<code>LinearLayout</code> is a view group (a subclass of [http://developer.android.com/reference/android/view/ViewGroup.html <code>ViewGroup</code>]) that lays out child views in either a vertical or horizontal orientation, as specified by the <code>android:orientation</code> attribute. Each child of a LinearLayout appears on the screen in the order in which it appears in the XML.
<code>LinearLayout</code> is a view group (a subclass of [http://developer.android.com/reference/android/view/ViewGroup.html <code>ViewGroup</code>]) that lays out child views in either a vertical or horizontal orientation, as specified by the <code>android:orientation</code> attribute. Each child of a LinearLayout appears on the screen in the order in which it appears in the XML.


<!--T:8-->
Two other attributes, <code>android:layout_width</code> and <code>android:layout_height</code>, are required for all views in order to specify their size.
Two other attributes, <code>android:layout_width</code> and <code>android:layout_height</code>, are required for all views in order to specify their size.


<!--T:9-->
Because the <code>LinearLayout</code> 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 <code>"match_parent"</code>. This value declares that the view should expand its width or height to match the width or height of the parent view.
Because the <code>LinearLayout</code> 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 <code>"match_parent"</code>. This value declares that the view should expand its width or height to match the width or height of the parent view.


<!--T:10-->
For more information about layout properties, see the Layout guide.
For more information about layout properties, see the Layout guide.
</translate>
</translate>


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

Version vom 16. Dezember 2015, 19:41 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

  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.