Zum Inhalt springen

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

Diese Seite wurde zum Übersetzen freigegeben
(Update auf letzte Version)
(Diese Seite wurde zum Übersetzen freigegeben)
Zeile 48: Zeile 48:
For more information about layout properties, see the Layout guide.
For more information about layout properties, see the Layout guide.


== Add a Text Field ==
== 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.
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.
# 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 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>.
# 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:
The <code><EditText></code> element should read as follows:
</translate>
</translate>
Zeile 65: Zeile 67:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<translate>
<!--T:14-->
Here are the <EditText> attributes you added:
Here are the <EditText> attributes you added:


=== [http://developer.android.com/reference/android/view/View.html#attr_android:id <code>android:id</code>] ===
=== [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).
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 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.
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>] ===
=== [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.
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>] ===
=== [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.
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 ==
== 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."
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>.
# 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>"edit_message"</code> with the value, "Enter a message".
Zeile 87: Zeile 91:
# Remove the line for the <code>"hello world"</code> string.
# Remove the line for the <code>"hello world"</code> string.


<!--T:20-->
The result for <code>strings.xml</code> looks like this:
The result for <code>strings.xml</code> looks like this:


Zeile 102: Zeile 107:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<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.
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.
For more information about using string resources to localize your app for other languages, see the Supporting Different Devices class.


== Add a Button ==
== Add a Button == <!--T:23-->
# In Android Studio, from the <code>res/layout</code> directory, edit the <code>activity_my.xml</code> file.
# 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.
# 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.
Zeile 132: Zeile 139:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<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.
'''Note''': This button doesn't need the <code>android:id</code> attribute, because it won't be referenced from the activity code.
</translate>
</translate>
[[Datei:edittext_wrap.png|thumb|<translate>The EditText and Button widgets have their widths set to "wrap_content".</translate>]]
[[Datei:edittext_wrap.png|thumb|<translate><!--T:25-->
The EditText and Button widgets have their widths set to "wrap_content".</translate>]]
<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.
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.
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.
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.
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 ==
== 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:
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>.
# 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>.
# Also, assign <code><EditText></code> element's <code>layout_width</code> attribute a value of <code>0dp</code>.
Zeile 158: Zeile 172:
</syntaxhighlight>
</syntaxhighlight>
<translate>
<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.
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 activity_my.xmllayout file should now look:
Here’s how your complete activity_my.xmllayout file should now look:
</translate>
</translate>
Zeile 183: Zeile 199:
<translate>
<translate>


== Run Your App ==
== 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:
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  .
# In Android Studio, from the toolbar, click Run  .
# Or from a command line, change directories to the root of your Android project and execute:
# Or from a command line, change directories to the root of your Android project and execute:
Zeile 191: Zeile 208:
#: <code>adb install bin/MyFirstApp-debug.apk</code>
#: <code>adb install bin/MyFirstApp-debug.apk</code>


<!--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.
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}}
{{Android Training/ Vorlage:Attribution}}
11.008

Bearbeitungen

Cookies helfen uns bei der Bereitstellung von Android Wiki. Durch die Nutzung von Android Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.