Diskussion:Android Training/Eine einfache Benutzeroberfläche erstellen: Unterschied zwischen den Versionen

keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 130: Zeile 130:
</source>
</source>
Die Höhe (height) und die Breite (width) des Buttons sind auf "wrap_content" gesetzt, so ist der Button nur so groß wie es für den Text im Button nötig ist. Dieser Button benötigt nicht das android:id Attribut, weil er vom activity Code nicht verwendet wird.
Die Höhe (height) und die Breite (width) des Buttons sind auf "wrap_content" gesetzt, so ist der Button nur so groß wie es für den Text im Button nötig ist. Dieser Button benötigt nicht das android:id Attribut, weil er vom activity Code nicht verwendet wird.
== 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.
[[Datei:edittext_wrap.png|thumb|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 and there's extra space left on the screen. So, it'd be nice to fill that width using the text field. LinearLayout enables such a design with the weight property, which you can specify using the android:layout_weight attribute.
Das Layout ist nun so entwickelt, das beide Elemente, das EditText und das Button-Element, nur so groß sind, wie es für die Darstellung des Textes nötig ist, wie das Bild 2 Darstellt.
[[Datei:edittext_wrap.png|thumb|The EditText and Button widgets have their widths set to "wrap_content". Das EditText und das Button Element haben den Wert "wrap_content"]]
Dies funktioniert für den Button einwandfrei, nicht aber für das Textfeld, weil der User manchmal auch längere Sachen einträgt und noch Extraplatz auf dem Bildschirm ist. So wäre es schön, wenn das Textfeld diesen Platz ausfüllt. LinearLayout erlaubt ein Design mit dem weight Eigenschaft, welches du mit android:layout_weight Attribut einstellen kannst.
The weight value allows you to specify the amount of remaining space each view should consume, relative to the amount consumed by sibling views, just like the 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 gets 2/3 of the remaining space and the second view gets the rest. If you give a third view a weight of 1, then the first view now gets 1/2 the remaining space, while the remaining two each get 1/4.
Der weigth-Wert erlaubt es dir die Größe einzustellen, wie viel Platz jedes Element nutzen darf, relativ zu dem Platz, welcher von anderen Elementen verwendet wird, ungefähr, wie bei einem Rezept für ein Getränk: "2 Teile Wodka, 1 Teil Kaffeelikör", bedeutet 2/3 vom Getränk ist Vodka. Zum Beispiel, wenn ein Element als weight-Wert 2 und ein anderes Element ein weight-Wert von 1 hat, die Summe ist 3, das erste Element bekommt 2/3 vom freien Platz und das zweite Element bekommt den Rest. Wenn du ein drittes Element mit einem Wert von 1 definierst, dann wird das 1. Element nun 1/2 Teil vom freien Platz bekommt, und die beiden übrigen Teile bekommen jeweils 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 each view is given the space it requires. So, to fill the remaining space with the EditText element, give it a weight of 1 and leave the button with no weight.
Der default-Wert für weight ist 0, also, wenn du nur ein Element mit einem Wert größer 0 definierst, so wird dieses Element den kompletten freien Platz verwenden, der nachdem alle Elemente den benötigten Platz zugewiesen wurden, übrig ist. Um den restlichen Platz mit dem EditText Element zu füllen, setzten wir den weight-Wert auf 1 und lasse den Button ohne weight.
<source lang="xml">
<EditText
        android:layout_weight="1"
        ... />
</source>
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.
<source lang="xml">
<EditText
        android:layout_weight="1"
        ... />
</source>
11.008

Bearbeitungen