Android Training: Building a Simple User Interface

Aus Android Wiki
Version vom 16. Dezember 2015, 18:56 Uhr von FuzzyBot (Diskussion | Beiträge) (Neue Version von externer Quelle importiert)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Warning: Display title "Android Training: Building a Simple User Interface" overrides earlier display title "Android Training/Building a Simple User Interface".

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.