First, the layout file implementation steps:
1. Create a demo_layout.xml layout file in the Res/layout directory,
2, using relativelayout nested a linearlayout for layout, add a TextView and two button, and set properties.
3. Create a firstlayoutactivity class, in the Oncreateview () method, instantiate the component with Findviewbyid ().
4.Activity Jump:
1.1 Create an activity, and register
1.2 Specify from which activity to jump to the new activity by intent
1.3 Start with Activity's startactivity (Intent Intent) method
5. Button tap to listen
1.1 In the layout file, deploy a button.
1.2 Add an ID to the button to find an instance of the button in the activity using Findviewbyid ().
1.3 Create a Click to listen View.onclicklistener ()
1.4 Call the button's Setonclicklistener () method to set a listener for the button
Ii. type of layout:
1.AbsoluteLayout, absolute layout, using the coordinate system Specification screen, the origin point in the upper left corner, X right is the positive direction, y downward is the positive direction.
1.1 Key attributes:
Layout_x,layout_y, when you place a control in Absolutelayout, these two properties are unique to absolutelayout and can only be used by controls in Absolutelayout. They are used to determine the placement coordinates of the specified control (the coordinates of the upper-left corner of the control).
1.2 Advantages and Disadvantages:
Pros: Use Absolutelayout to be the most free to place controls on our interface.
Cons: Because Absolutelayout uses a coordinate system to determine the placement of child controls, but because of the fragmentation of the Android platform screen, the user's screen resolution can not be determined, then the use of this type of layout, may make users feel different, affect the user experience.
2.LinearLayout, linear layout, all sub-controls are lined up neatly.
2.1 Key attributes:
LinearLayout Properties:
android:orientation= "Vertical" ["Horizontal"]
Represents the placement direction of the child controls in the LinearLayout, vertical vertically (top to bottom). Horizontal horizontally (from left to right).
Android:baselinealigned= "true" ["false"]
Indicates that the placement of the child controls in the LinearLayout is aligned according to the baseline.
True is aligned, false is not aligned.
android:baselinealignedchildindex= "Childindex"
Represents the baseline of which child control is aligned.
Android:gravity= "Center_horizontal"
Indicates the direction of the child control alignment, left, right, top, bottom, center ...
Child control Properties:
Android:layout_weight= "1.0"
This property is valid only in LinearLayout, which represents the size that the child control needs to occupy, and is used to split the space of the parent control with other controls.
2.2 Advantages and Disadvantages:
Pros: Linear layout with minimal impact on screen resolution. The child controls are arranged neatly.
Cons: Child controls can only be placed in a number of ways, and may not fit the needs of a wide variety of functional interfaces in commercial projects.
3.FrameLayout, frame layout, all controls placed inside are layered, one layer of controls. The controls are placed in the topmost layer. If the top-level control is not large enough, you can see the other controls below him.
3.1 Key attributes:
Framelayout Properties:
Android:foreground, you can use this property to add an image or color to the front of the entire layout.
Android:foregroundgravity, adjusts the alignment direction of the picture set by the Android:foreground.
3.2 Advantages and disadvantages
Pros: There are multiple animation controls implemented by Framelayout, which shows that Framelayout is a good fit for animating the functionality of the interface implementation.
Disadvantage: The constraint is strong, the control is placed only this one, the internal control of the means of operation is relatively small.
4.RelativeLayout, relative layout, determines the position of the new control that is now placed, either through the edge of the parent control or the position of the control that was previously set
4.1 Properties of child controls:
Android:layout_alignparenttop
Android:layout_alignparentbottom
Android:layout_alignparentleft
Android:layout_alignparentright, pasted to the edge of the specified parent control.
Android:layout_toleftof
Android:layout_torightof
Android:layout_tostartof
Android:layout_toendof, placed in the specified direction of the control with the specified ID.
Android:layout_above
Android:layout_below, placed above or below the control with the specified ID.
Android:layout_alignleft
Android:layout_alignright
Android:layout_aligntop
Android:layout_alignbottom, aligned with the control edge of the specified ID.
Android:layout_centerinparent, place our controls in the middle of the parent control.
4.2 Advantages and disadvantages
Pros: The internal sub-controls are positioned relative to each other, and their relative positions are unchanged when the screen resolution changes dramatically. This layout type can greatly reduce the difference of the interface effect when our screen changes.
Cons: When the relative relationship between our controls is established, it's not easy to make changes again.
5.Tablelayout, table layout, but must be used in conjunction with TableRow, otherwise it is a vertically arranged linear layout.
5.1 Key attributes:
Tablelayout:
android:stretchcolumns= "0" stretches the column with the column number starting from 0.
android:collapsecolumns= "0" hidden column, ibid.
android:shrinkcolumns= "0" contraction column, ibid.
TableRow:
Android:weightsum= "5", this property is used with the control's android:layout_weight= "1" to set the size of all controls on a row. (only the new version can be used, after the 4.0 version can be used)
6.GridLayout, Grid layout, no need to tablerow so troublesome
6.1 Key methods
Android:columncount= "3"
Android:rowcount= "5"
You can easily set the number of rows and columns of our grid layout by the above two methods.
Child controls can also use android:layout_column= "1" To set the child control to be ranked in the first few.
Three, the common control:
1. button, buttons
There are 3 commonly used buttons:
1.1 You can use the button when you only need to place text messages on the buttons.
1.2 You can use ImageButton when you only need to place the picture on the button.
1.3 You can use the button when you need to place text and pictures on the buttons at the same time.
button with text message only:
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/button_text"
.../>
Buttons that contain only pictures:
<imagebutton
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/button_icon"
.../>
Buttons that contain both text and pictures:
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/button_text"
android:drawableleft= "@drawable/button_icon"
.../>
1.4 Common Properties:
1. Use the Android:onclick property to easily respond to a button's Click event.
1.1. Add the Android:onclick attribute to the button tag in the XML file.
1.2. Add a fixed-format method to the activity that loads the XML file:
public void Response Method name (view view) {}
1.3. Fill the name of the response method into the value of Android:onclick.
use Android:backgroud to set a new appearance.
New appearance picture Making a selector:
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_pressed= "true" android:drawable= "@drawable/pic1" ></item>
<item android:state_pressed= "false " android:drawable= "@drawable/pic2" ></item>
</selector>
1.5 Onclicklistener
1,5.1 Gets the button object.
1.5.2. Create an View.onclicklistener object.
1.5.3. Then hand the View.onclicklistener object to the button object.
2. EditText: Can input text box, this control can let the user enter text information.
<edittext
Android:id= "@+id/email_address"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"/>
2.1 Properties:
Android:hint= the "@string/email_hint" property: Edit the box to add a hint message.
Android:inputtype= "textemailaddress" to adjust the soft keyboard style, get only the number or easy to enter the email address of the soft keyboard.
Android:imeoptions= "Actionsend" to customize the soft keyboard confirmation button.
Android:password= "true" to enter information in ciphertext form. When false is changed back, it is entered in clear text.
After we have added the Imeoptions attribute, we need a listener to listen for the button's response:
EditText EditText = (EditText) Findviewbyid (R.id.search);
Edittext.setoneditoractionlistener (New Oneditoractionlistener () {
@Override
public boolean oneditoraction (TextView v, int ActionId, keyevent event) {
Boolean handled = FALSE;
if (ActionId = = editorinfo.ime_action_send) {
handled = TRUE;
}
return handled;
}
});
In addition to EditText, we also have Autocompletetextview as input to the text information control
3. TextView, text box.
3.1 Common Properties:
Android:text Text information
Android:textsize Font Size (unit SP)
Android:textcolor Font Color
Android:autolink can make the phone number, URL, and mailbox in the text look like a hyperlink.
Android:lines Number of rows
Android:maxlength Maximum number of words
4. CheckBox, Multi-box.
4.1 You can use the set onclick to add a click response written in the activity
Method.
4.2 We can use the multi-marquee listener: Oncheckedchangelistener
4.3 Custom styles:
Set selector on the Android:button property.
5. RadioButton, Radio box.
This control cannot be used alone and needs to be used with Radiogroup. Add a oncheckedchangelistener to Radiogroup to listen to our choice of listening.
Be sure to note that this listener and multi box are not the same, pay attention to their package name!
6.datepicker,timepicker Date and time
The DatePicker and timepicker are defined in the layout file, then the system time is obtained through the Canlendar class, and the date is passed to DatePicker through the Init method.
and set the Ondatechangedlistener to monitor the date changes, when the time is changed, you need to set Setontimechangedlistener monitoring to set the time.
<datepicker
Android:id= "@+id/datepicker"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
<timepicker
Android:id= "@+id/timerpicker"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
7. ListView, List control, implementation of a data list, easy to modify data, while affecting the interface.
<listview
Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:entries= "@array/cities" >
</ListView>
7.1 Common Properties
android:divider= the picture or color of the "@color/mycolor" spacer Line
The width of the android:dividerheight= "3dip" spacer line is used to modify the spacer line between each item in the ListView.
Android:stackfrombottom= "True" starts at the bottom of the list.
Android:transcriptmode= "Alwaysscroll" always shows the new data.
Android:cachecolorhint= "@android: color/transparent" When sliding, the background is transparent.
7.2 Display the ListView using the adapter:
1. In the layout file, define a simple listview tag.
2. Implement a Baseadapter subclass object that we extend ourselves in the activity.
3. Find the ListView object in the layout file by Findviewbyid in the activity.
4. Set the adapter object to our ListView object.
The ListView can only load his child controls through the adapter.
Dynamic addition of 7.3 ListView dynamic Content
1. Update our data container with the data obtained by the user or the server.
2. Then call Adapter.notifydatasetchanged () to remind the ListView that the data has been updated,
The interface needs to be refreshed.
7.4 ListView Performance Optimization:
1. Multiplexing Convertview
In the GetView parameter list, we have a convertview that provides a way to optimize performance: Reusing the Convertview that just launched the screen range.
2. Reduce the number of Findviewbyid used
There is a tag property of type Object in Convertview, which stores an object on this property. This object is used to store a defined inner class object, which is saved
Convertview the child control object. It is also possible to reuse a child control object that was previously fetched when the Convertview is reused.
8.AdapterView
Adapter is an adapter that is used to connect two unrelated things to a bridge. At the same time, the adapter is used to connect our adapterview and data.
Adapterview we commonly use are: ListView (List control), GridView (Grid control),
Gallery (gallery).
Android Basics (i)