Android native annoying layout
The UI layout is an annoying thing to do when developing Android apps. Here is the main explanation of the interface layout in Android.
one, linear layout (LinearLayout)
The linear layout is divided into:
(1) Vertical linear layout;
(2) Horizontal line layout;
For both of these differences, just one attribute is the difference
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" >
Horizontal line of layout, android:orientation= "Horizontal" can be.
ii. Relative Layout (relativelayout)
General linear layout can not meet the needs of the actual project, is the general Web interface UI design, but also exist relative elements of some CSS style layout. The relativelayout parameters are: width,height,below,aligntop,toleft,padding, and Merginleft.
Key Source:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <textview android:id= "@+id/label" android:layout_width = "Fill_parent" android:layout_height= "wrap_content" android:text= "Please enter:"/> <edittext Android:id= "@+id/entry" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:background= "@android:d rawable/editbox_background" android:layout_below= "@id/label"/> <Button Android:id= "@+id/ok" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@id/entry" android:layout_alignparentright= "true" Android:layout_marginlef t= "10dip" android:text= "OK"/> <button android:layout_width= "Wrap_content" android:l Ayout_height= "Wrap_contenT "android:layout_toleftof=" @id/ok "android:layout_aligntop=" @id/ok "android:text=" Cancel "/> & Lt;/relativelayout>
Wherein, the android:layout_below= "@id/label" set the EditText under TextView; android:layout_below= "@id/entry" in the button Set the button to be located under EditText.
Instance effect:
third, form layout (tablelayout)
Tablelayout is made up of many TableRow, each of which defines a row. The Tablelayout container does not display the Row,column or cell border lines, each row has 0 or more cells, and each cell has a view object. A table consists of rows and columns of many cells, allowing cells to be empty. But cells cannot span columns, which is different from HTML.
<view android:layout_height= "2dip" android:background= "#FF909090"/><tablerow> < TextView android:text= "*" android:padding= "3dip"/> <textview android:text= "Import ..." android:padding= "3dip"/> </TableRow> <TableRow> <textview android:text= "*" android:padding= "3dip"/> <textview android:text= "Export ..." android:padding= "3dip"/> <textview android:text= "ctrl-e" android:gravity= "right" android:padding= "3dip"/> </TableRow>
Instance effect:
android:gravity= "Center" written explanation is weight ratio. The time is to let it center the display.
android:stretchcolumns= "1,2,3,4" means that the 1,2,3,4 column is automatically stretched.
Note
For the component to be centered, the layout should be as follows:
Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= " Center_vertical|center_horizontal "
TableRow Flat Breakdown
The width of each column is set to Android:layout_width= "0.0dip", and the android:layout_weight= "1" In each column is set
Because Ayout_weight is allocating the remaining space by weight, instead of allocating all the space by weight.
The code is as follows:
<tablerow android:paddingtop= "15px" > <button android:id= "@+id/register" android:text= "@string/register" android:layout_width= "0.0dip" android:layout_weight= "1" android:onclick= " Register "/> <button android:id=" @+id/cancel " android:layout_width=" 0.0dip " android: layout_weight= "1" android:text= "@string/button_cancel"/> </TableRow>
The view is as follows:
four, switch card (tabwidget)
Switch cards are often used on the options, similar to the phone book interface, with multiple tabs to toggle the display of different content. In this case, Tabhost is a container to hold tab tags, gettabhost method to get Tabhost object, through the AddTab method to add tab to the container. tab will generate an event when switching, and can listen to Setontabchangedlistener through tabactivity events.
"extension point" tabhostclass overview
A window view container that provides tabs (tab pages). This object contains two sub-objects: One is the label that the user can select to specify the tab page, and the other is the content that Framelayout uses to display the tab page. Individual elements typically control the use of this container object, rather than setting the value of the child element itself.
(Translator Madgoat Note: Even if you are using a single element, it is best to put it in the container object ViewGroup)
Inner class
Interface Tabhost.ontabchangelistener
interface defines the callback function that is called when the tab change is made
Interface Tabhost.tabcontentfactory
Generate tab contents when a tab is selected
Class Tabhost.tabspec
Separate tabs, each tab has a tab designator, content and tag tags for easy recording.
Key Source
<tabhost xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@android: Id/tabhost" android: Layout_width= "Fill_parent" android:layout_height= "fill_parent" > <linearlayout android:orientation = "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TABW Idget android:id= "@android: Id/tabs" android:layout_width= "Fill_parent" Android:layo ut_height= "Wrap_content"/> <framelayout android:id= "@android: Id/tabcontent" Android Oid:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <textview Android:id= "@+id/textview1" android:layout_width= "Fill_parent" Android:layout_heig ht= "Fill_parent" android:text= "This is a tab"/> <textview android: Id= "@+id/textview2" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" and Roid:text= "This is another tab"/> <textview android:id= "@+id/textview3" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" Android:tex T= "This is a third tab"/> </FrameLayout> </LinearLayout> </TabHost>
Processing class
Declares the Tabhost object Tabhost mtabhost; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Get Tabhost Object mtabhost = Gettabhost (); /* Add a label for Tabhost *///Create a new Newtabspec (NEWTABSPEC)//Set its label and icon (setindicator)//set content (SetContent) Mtabhost.addtab (Mtabhost.newtabspec ("Tab_test1"). Setindicator ("tab 1", Getresources (). Getdrawable (R . drawable.img1)). SetContent (R.id.textview1)); Mtabhost.addtab (Mtabhost.newtabspec ("Tab_test2"). Setindicator ("tab 2", Getresources (). Getdrawable (R.drawa BLE.IMG2)). SetContent (R.ID.TEXTVIEW2)); Mtabhost.addtab (Mtabhost.newtabspec ("Tab_test3"). Setindicator ("Tab 3", Getresources (). Getdrawable (R.drawa BLE.IMG3)). SetContent (R.ID.TEXTVIEW3)); Sets the background color of the Tabhost Mtabhost.setbackgroundcolor (ColOr.argb (150, 22, 70, 150)); Set Tabhost background Image resource//mtabhost.setbackgroundresource (R.DRAWABLE.BG0); Sets which label is currently displayed, the default subscript starting from 0 mtabhost.setcurrenttab (0); Tag switch event handling, Setontabchangedlistener Mtabhost.setontabchangedlistener (new Ontabchangelistener () {Publ IC void ontabchanged (String tabId) {Dialog Dialog = new Alertdialog.builder (examples_04_29activity.this) . Settitle ("hint"). Setmessage ("Currently selected:" +tabid+ "label") . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {p ublic void OnClick (dialoginterface dialog, int whichbutton) {dialog.cancel (); }}). Create ();//Creating Button dialog.show (); } }); }
Instance effect:
Reference Documents
1.http://blog.csdn.net/fancylovejava/article/details/45787729/
American and American pictures
Android Advanced (27) Android native annoying layout