Set text display
Widgets that inherit ViewText can set text display in the following ways: 1. Set it in Android XML: text = "xxxxx"; 2. Use widgets in the program. setText ("xxxx"); 3. In res/values/strings. set in xml, and then use R. string. xxx is used in Android or in programs. To adapt to multiple languages, we recommend that you use the third method. You only need to translate the stings. xml vocabulary.
Optionmenu
On the mobile phone, there is a Menu button. After pressing it, you can display OptionMenu, as shown below:
@ Override
Public BooleanOnCreateOptionsMenu(Menu ){
// Todo auto-generated method stub
//Add(INT groupid, int Itemid, int order, int titleres). For exit menuitem, 0 indicates groupid, 1 indicates Itemid, the two IDs will be used to trigger the callback function after the menu is pressed to determine which menuitem is specific, and the third one indicates the visiting order. Here the first one is exit, and the second one is about, the fourth parameter leads to strings. content defined in XML.
Menu. Add (0, 1, R. String. menu_exit );
Menu. add (0, 2, R. string. menu_about );
Return super. onCreateOptionsMenu (menu );
}
After creating the Menu, you need to trigger the callback function, which is also an override method, as follows:
@ Override
Public booleanOnOptionsItemSelected(MenuItem item ){
// TODO Auto-generated method stub
If (item.GetItemId() = 1) {// in this example, we only have one group. You only need to check the itemId.
Finish();
} Else if (item.GetItemId() = 2 ){
......
}
Return super. onOptionsItemSelected (item );
}
Linearlayout
From top to bottom, or left to right. It is a box model, which is arranged in sequence based on squares. RadioGroup is an inherited item of LinearLayout.
Orientation
Android: orientation is used in android XML with the value horizontal or vertical. The layout is horizontal from left to right, or vertical from top to bottom. In java programs, we can change the direction through setOrientation (), for example, setOrientation (LinearLayout. HORIZONTAL );
Fill Model
A widget has the most appropriate size according to its content, that is, the natural size. However, we only need to typeset according to the container size. In LinearLayout, android: layout_width and android: layout_height must be set in three ways:
1) specify the size. For example, 125px indicates 125 pixels.
2) wrap_content, which is the size of natual
3) fill_parent: fill the remaining size of the container.
Fill_parent will use all the remaining space, even if there is a widget behind it, it will fill up the space. We do not want this. We want the remaining widgets to be effectively arranged, but the remaining space after other widgets are arranged. We will use the layout_weight attribute.
Weight
Layout_weight and wrap_content are used together. By default, layout_weight is 0, that is, size is natural size. (If it is combined with fill_parent, the results cannot be predicted, so pay special attention to it) if we want the widget to use the remaining space, we can set a valid value for layout_weight. Here is the weight. If Multiple widgets exist, the remaining space is allocated based on the weight. If all layout_weight is the same, these widgets perform an equal score on the remaining space. Sometimes we can see that the space is not necessarily allocated strictly according to the set weight ratio, and the natural size must be supported, so there may be adaptation.
Gravity
Gravity can be used for linear layout from top to bottom, or from left to right. align mode is provided, not in this order. For android: layout_width = "wrap_content", android: layout_gravity can be set to left, center_horizontal, and righ. In JAVA code, you can use setGravity (), such as setGravity (Gravity. LEFT); setGravity (Gravity. CENTER_HORIZONTAL );
It should be noted that the widget also has corresponding properties, android: gravity, which refers to the position where the text in the widget is discharged in the widget, such as android: gravity = "right ".
The figure on the right shows an example of combining weight and gravity with a button.
<Edittext
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: singleline = "false"
Android: text = "test1"
Android: layout_gravity = "left"
/>
<Edittext
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "2"
Android: singleLine = "false"
Android: text = "test2/ncenter_horizontal"
Android: layout_gravity = "center_horizontal"
/>
<EditText
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: singleLine = "false"
Android: text = "test3/nLine_2/nLine_3"
Android: layout_gravity = "right"
/>
If we change the third android: layout_gravity = "right" to android: gravity = "right", the result is as follows:
Pad and Margin
By default, Widgets are closely arranged. If you need to leave an edge, you can use padding ,. Use android: padding, android: paddingLeft, android: paddingRight, android: paddingTop, and android: paddingBottom in XML. In Java code, setPadding () can be used for processing. For example, android: paddingTop = "30px ".
Note that if you set the background Android: background, the background will not be visible in the widget and padding areas. That is, the widget actually contains the padding size. To avoid this problem, we can use margin, such as Android: layout_margintop, Android: layout_marginleft = "100px"
In general, padding can be understood as the widget's internal border, while margin can be understood as the outer border.
Related links:
My android development articles