Learn how to develop common layouts for Android

Source: Internet
Author: User

There are a few of the ways in which we commonly use layouts in Android development:

1.LinearLayout (Linear layout): (There can only be one control inside, and cannot design the position of the control, the control will be placed in the upper left corner)

Linear layouts are divided into horizontal linear and vertical linear properties, respectively: android:orientation= "horizontal" android:orientation= "vertical".

2.RelativeLayout (Relative layout): (You can put multiple controls inside, but only one control on a line)

Additional classes of Relativelayout properties are available for your reference:

First Class: Property value is True or False

Android:layout_centerhrizontal Horizontal Center

Android:layout_centervertical Vertical Center

Android:layout_centerinparent is completely centered relative to the parent element

Android:layout_alignparentbottom snaps to the bottom edge of the parent element

Android:layout_alignparentleft snaps to the left edge of the parent element

Android:layout_alignparentright snaps to the right edge of the parent element

Android:layout_alignparenttop snaps to the top edge of the parent element

Android:layout_alignwithparentifmissing, if you can't find the brother element, use the parent element as a reference.

Second class: The attribute value must be the reference name of the id "@id/id-name"

Android:layout_below at the bottom of an element

Android:layout_above on top of an element

Android:layout_toleftof on the left side of an element

Android:layout_torightof on the right side of an element

Android:layout_aligntop aligns the top edge of this element with the top edge of an element

Android:layout_alignleft aligns the left edge of this element with the left edge of an element

Android:layout_alignbottom aligns the bottom edge of this element with the bottom edge of an element

Android:layout_alignright aligns the right edge of this element with the right edge of an element

Class III: Attribute values are specific pixel values, such as 30dip, 40px

Android:layout_marginbottom distance from the bottom edge of an element

Android:layout_marginleft distance from the left edge of an element

Android:layout_marginright distance from the right edge of an element

Android:layout_margintop distance from the top edge of an element

3.TableLayout (Table layout): (This is used in conjunction with TableRow, much like the table in HTML)

This table layout is not as flexible as the table in HTML, but only through the TableRow property to control its rows and columns, there are several controls in a few columns (typically). Such as:

<TableLayout>

<TableRow>

<EditText></EditText>

<EditText></EditText>

</TableRow>

<TableRow>

<EditText></EditText>

<EditText></EditText>

</TableRow>

</TableLayout>

Represents a table of two columns in two rows.

android:gravity= "Center" written explanation is weight ratio. The time is to let it center the display. It can also dynamically add each row inside each column. As shown in the following code:

/* Find table objects by ID */

Tablelayout tablelayout = (tablelayout) Findviewbyid (R.ID.TABLE01);

/* Create a Column object */

TableRow TableRow = new TableRow (this);

/* Text Box Object */

TextView temp = new TextView (this);

Temp.settext ("Value of text");

/* Add this text to the column */

Tablerow.addview (temp);

android:stretchcolumns= "1,2,3,4" means that the 1,2,3,4 column is automatically stretched.

4.AbsoluteLayout (Absolute layout): (You can put multiple controls inside, and you can define the location of the control's X, y)

5.FrameLayout (Frame layout): (You can put more than one control, but the position of the control is relative position)

The controls inside it are all displayed as one of the controls superimposed on the previous control, and all the elements are placed in the top left corner. Such as:

<framelayout android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content" android:layout_weight= "1" >

<imageview android:id= "@+id/iv1" android:layout_width= "Wrap_content"

android:layout_height= "wrap_content" android:visibility= "Invisible"

android:src= "@drawable/lotusleaf" ></ImageView>

<imageview android:id= "@+id/f1" android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content" android:src= "@drawable/frog_right"

android:visibility= "Invisible" ></ImageView>

</FrameLayout>

Represents a control with ID F1 superimposed on a control with ID IV1

(LinearLayout and Relativelayout should be more than two of them.) Absolutelayout is less used because it is laid out according to the absolute position of the screen, and the position of the control changes if the screen size changes. This is equivalent to the absolute layout of the HTML, it is generally not recommended)

Precautions:

1, each layout do not mess with their own attributes. For example, the android:layout_x and android:layout_y belonging to the Absolutelayout layout are used for linearlayout layout or relativelayout layout, or the relativelayout layout Below, Rightof, and other properties are applied to other layouts. Although this will not be an error, but it is a white waste of feelings of the work, simply do not achieve the effect we need.

2, about Android:layout_width= "fill_parent" and android:layout_height= "Wrap_content", this is the setting for each layout width and height. Wrap_content can indicate that the width or height of the layout changes depending on the control, similar to the auto-set width and height, fill_parent the layout to fill the entire screen, and another match_parent, which is essentially the same as fill_parent, and from API Level8 begins to replace Fill_parent.

Properties of the TextView:

Android:autolink//Sets whether text is displayed as a clickable link when the text is a URL link/email/phone number/map. Selectable VALUES (None/web/email/phone/map/all)

Android:autotext//If set, the spelling correction of the input values will be performed automatically. No effect here, when display input method and input the role

Android:buffertype//Specifies the type of text obtained by GetText (). The option editable is similar to the StringBuilder append character, which means that the Append method can be called to set the text content after gettext. Spannable can use styles in a given character area

Android:capitalize//Set the English letter capitalization type. No effect here, need to pop input method to see, see EditView This property description

Android:cursorvisible//Set cursor to show/hide, default display

Android:digits//sets which characters are allowed to be entered. such as "1234567890.+-*/% ()"

Android:drawablebottom//Output a drawable at the bottom of text, slice. If you specify a color, the background of text is set to that color, and the latter is overridden when used with background

Android:drawableleft//Output a drawable on the left of text, slice

Android:drawablepadding//Set the interval between text and drawable (picture), used with Drawableleft, Drawableright, Drawabletop, Drawablebottom, Can be set to negative, use no effect alone

Android:drawableright//Output a drawable to the right of text

Android:drawabletop//Output a drawable directly above the text

Android:editable//Set whether editable

Android:editorextras//Set additional input data for text

Android:ellipsize//sets how the control is displayed when the text is too long. The following values are set: "Start"-the ellipsis is displayed at the beginning; End "--the ellipsis appears at the end; Middle "--the ellipsis appears in the middle; Marquee "--display in the form of a marquee (animated lateral movement)

Android:freezestext//Set the contents of the saved text and the position of the cursor

Android:gravity//Set text position, as set to "center", text will be centered

Android:hinttext//Is the text message displayed when empty, you can set the color of the hint information through Textcolorhint. This property is used in EditView, but can also be used here

Android:imeoptions//Additional functions, set the lower right corner of the IME action associated with the edit box action, such as Actiondone in the lower right corner will display a "done", and not set the default is a carriage return symbol. This is explained in detail in EditView, which is useless here

Android:imeactionid//Set IME action ID

Android:imeactionlabel//Set IME action label

Android:includefontpadding//Set text to include top and bottom extra whitespace, default true

Android:inputmethod//Specifies the input method for text and requires a fully qualified name (full package name). For example: Com.google.android.inputmethod.pinyin, but the error is not found here

Android:inputtype//sets the type of text used to help the input method display the appropriate keyboard type. Detailed in the EditView, no effect here

android:linksclickable//Set link whether click Connect, even if set Autolink

Android:marqueerepeatlimit//In the case of Ellipsize specifying marquee, sets the number of times to repeat the scroll when set to Marquee_forever for unlimited

Android:ems//sets the width of the TextView to n characters. Test here for a Chinese character width

Android:maxems//sets the width of the textview to a maximum of n characters. Overwrite EMS option when used in conjunction with EMS

Android:maxlength//Limit display of text length, out of section not shown

Android:lines//Set the number of lines of text, set two lines to display two lines, even if the second row has no data

Android:maxlines//sets the maximum number of lines of text, used in conjunction with width or layout_width, exceeds the partial wrap, and the number of lines beyond will not be displayed

Android:minlines//sets the minimum number of lines of text, similar to lines

Android:linespacingextra//Set line spacing

Android:linespacingmultiplier//sets a multiple of the line spacing. such as "$

Android:numeric//If set, the TextView has a digital input method. This is useless here, the only effect after setting is TextView has the click Effect, this property will be explained in detail in Edtiview

Android:password//With small dots "." Display text Android:phonenumber set as input for phone numbers

Android:privateimeoptions//Set IME option, useless here, will be discussed further in EditText

android:scrollhorizontally//If the text exceeds the width of the TextView, the horizontal pull bar appears

Android:selectallonfocus//If the text is selectable, let him get the focus instead of moving the cursor to the beginning or end of the text. No effect after setting in TextView

Android:shadowcolor//Specifies the color of the text shadow that needs to be used with Shadowradius

ANDROID:SHADOWDX//Set shadow horizontal coordinates start position

Android:shadowdy//Set shadow vertical coordinates start position

Android:shadowradius//Sets the radius of the shadow. Set to 0.1 to become the color of the font, generally set to 3.0 effect is better

Android:singleline//Set single-line display. If used with Layout_width, when the text cannot be displayed in its entirety, it is followed by "...". such as android:text= "Test_ singleline" android:singleline= "true" android:layout_width= "20DP" will show only "t ...". If you do not set Singleline or set to False, the text wraps automatically android:text the text is set to display. Android:textappearance to set the appearance of text. As "? Android:attr/textappearancelargeinverse" here refers to the system comes with a look,? Indicates whether the system has this appearance, otherwise use the default appearance. The values you can set are as follows: textappearancebutton/textappearanceinverse/textappearancelarge/textappearancelargeinverse/ Textappearancemedium/textappearancemediuminverse/textappearancesmall/textappearancesmallinverse

Android:textcolor//Set text color

Android:textcolorhighlight//Background of selected text, blue by default

Android:textcolorhint//sets the color of the message text, which is grayed out by default. Used in conjunction with hint.

The color of the Android:textcolorlink//text link.

Android:textscalex//sets the spacing between text, which defaults to $.

Android:textsize//Set text size, recommended unit of measure "SP", such as "15SP"

Android:textstyle//Set glyph [Bold (bold) 0, Italic (italic) 1, bolditalic (coarse and oblique) 2] You can set one or more, with "|" Separated

Android:typeface//Set text font, must be one of the following constant values: normal 0, sans 1, serif 2, monospace (equal width font) 3]

Android:height//Set height of text area, support unit of measure: px (pixels)/dp/sp/in/mm (mm)

Android:maxheight//Set the maximum height of the text area

Android:minheight//Setting the minimum height of the text area

Android:width//Set width of text area, support unit of measure: px (pixels)/dp/sp/in/mm (mm), see here for differences with Layout_width

Android:maxwidth//sets the maximum width of the text area

Android:minwidth//Setting the minimum width of the text area Android layout Properties Relativelayout Some important properties to use: First Class: Property value is True or False

Android:layout_centerhrizontal//Horizontal Center

Android:layout_centervertical//Vertical Center

Android:layout_centerinparent//fully centered relative to parent element

Android:layout_alignparentbottom//Snaps to the bottom edge of the parent element

Android:layout_alignparentleft//Snaps to the left edge of the parent element

Android:layout_alignparentright//Snaps to the right edge of the parent element

Android:layout_alignparenttop//Snaps to the top edge of the parent element

Android:layout_alignwithparentifmissing//If the corresponding sibling element cannot be found, then the parent element is used as a reference. Class II: The attribute value must be the referenced name of the id "@id/id-name"

Android:layout_below//At the bottom of an element

Android:layout_above//At the top of an element

Android:layout_toleftof//On the left side of an element

Android:layout_torightof//On the right side of an element

Android:layout_aligntop//Align the top edge of this element with the top edge of an element

Android:layout_alignleft//The left edge of this element is aligned with the left edge of an element

Android:layout_alignbottom//The bottom edge of this element and the bottom edge of an element are aligned

Android:layout_alignright//The right edge of this element and the right edge of an element are aligned to the third class: The attribute value is a specific pixel value, such as 30dip,40px

Android:layout_marginbottom//distance from the bottom edge of an element

Android:layout_marginleft//distance from the left edge of an element

Android:layout_marginright//distance from the right edge of an element

Android:layout_margintop//distance from the top edge of an element edittext the Android:hint setting in the input box when EditText is empty

The

Android:gravity//property is a qualification for the view content. For example, a button above the text. You can set the text on the left side of the view, on the right, and so on. Take the button for example, android:gravity= "The text on the button above the right android:layout_gravity android:layout_gravity is used to set the position of the view relative to the parent view. Like a button in LinearLayout, You can set the button on the left, right, and so on. In the case of button, android:layout_gravity= "Right" is the button on Android:layout_ Alignparentright aligns the right end of the current control with the right end of the parent control. This property value can only be true or FALSE, which is false by default. Android:scaleType:android:scaleType is a size that controls how the picture resized/moved to the ImageView. Imageview.scaletype/android:scaletype value difference: Center/center by the original size of the picture is displayed, when the picture is longer/wider than the length/width of the view, then the center portion of the image is captured Center_ Crop/centercrop proportionally enlarges the size of the image so that the image is long (wide) equal to or greater than the length (width) of the view center_inside/centerinside the contents of the picture are displayed in a complete center. Zoom in/out to the width of the view by scaling it down or the original size so that the picture is long/wide equal to or less than the length/width of the view fit_center/fitcenter the picture proportionally to the image, and the center display fit_end/fitend the picture proportionally. Zoom out to the width of the view, displayed in the lower part of the view fit_start/fitstart the picture proportionally to the width of the view, displayed in the upper part of the view fit_xy/fitxy the image is not scaled up/down to the view size display The Matrix/matrix is drawn with a matrix and is displayed by zooming in and out of the image. * * Note that the image in the Drawable folder cannot be capitalized

Edittext Properties: Edittext inheritance Relationship: View-->textview-->edittext. EditText a lot of properties, here are a few: android:layout_gravity= "center_vertical"//settings control display location: Default top, here is centered, and bottom android:hint= " Please enter a number! "//Set the information displayed on the Space android:numeric=" integer "//settings can only enter integers, if the decimal is: decimal android:singleline=" true "//Set single-line input, Once set to True, the text is not wrapped automatically. Android:password= "true"//settings can only enter password Android:textcolor = "#ff $"//Font Color android:textstyle= "bold"//font, bold, italic, Bolditalic android:textsize= "20dip"//Size android:capitalize = "characters"//write in capital letters android:textalign= "center"// EditText does not have this attribute, but TextView has, centered android:textcolorhighlight= "#cccccc"//The background of the selected text, the default is blue android:textcolorhint= "#ffff00 "//Set the color of message text, default to Gray android:textscalex=" 1.5 "//control the spacing between words android:typeface=" monospace "//font, normal, sans, serif, Monospace android:background= "@null"//space background, not here, refers to the transparent android:layout_weight= "1"//weight, control the position between controls, it is very useful to control the size of the control display. Android:textappearance= "? Android:attr/textappearancelargeinverse"

1.EditText does not eject the software keyboard by default

Method One:

Which activity to select in Androidmainfest.xml, set Windowsoftinputmode property to Adjustunspecified|statehidden

Android:windowsoftinputmode= "Adjustunspecified|statehidden"

Method Two:

Let EditText lose focus, use EditText's Clearfocus method

Edit.clearfocus ();

Method Three:

Force Hide Android IME window

For example: EditText edit= (EditText) Findviewbyid (R.id.edit);

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service);

Imm.hidesoftinputfromwindow (Edit.getwindowtoken (), 0);

2.EditText always does not eject the software keyboard

Example: EditText edit= (EditText) Findviewbyid (R.id.edit);

Edit.setinputtype (Inputtype.type_null);

button inherits from view, view has properties it can be used <?xml version= "1.0" encoding= "Utf-8"?>

<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >

<item android:state_pressed= "true" > (here the style is displayed when the button is pressed)

<shape>

<gradient

Android:startcolor= "@drawable/gray"

Android:endcolor= "@drawable/white"

android:angle= "*"/>

<stroke

Android:width= "*DP"

Android:color= "@drawable/teal"/>

<corners

android:radius= "*DP"/>

<padding

android:left= "**DP"

android:top= "*DP"

android:right= "**DP"

android:bottom= "*DP"/>

</shape>

</item>

<item android:state_focused= "true" > (here the style is displayed when moving to the button)

<shape>

<gradient

Android:startcolor= "@drawable/silver"

Android:endcolor= "@drawable/springgreen"

android:angle= "*"/>

<stroke

Android:width= "*DP"

Android:color= "@drawable/teal"/>

<corners

android:radius= "*DP"/>

<padding

android:left= "**DP"

android:top= "*DP"

android:right= "**DP"

android:bottom= "*DP"/>

</shape>

</item>

<item> (here the style is displayed when the button is normal)

<shape>

<gradient

Android:startcolor= "@drawable/silver"

Android:endcolor= "@drawable/snow"

android:angle= "*"/>

<stroke

Android:width= "*DP"

Android:color= "@drawable/teal"/>

<corners

android:radius= "*DP"/>

<padding

android:left= "**DP"

android:top= "*DP"

android:right= "**DP"

android:bottom= "*DP"/>

</shape>

</item>

</selector>

Note:

<padding

android:left= "**DP"

android:top= "*DP"

android:right= "**DP"

android:bottom= "*DP"/>

Here left and right controls whether the font on the button is the distance from the edge of the buttons, whether the control button is long or short, and where top and bottom control the distance between the button's upper and lower edges, that is, whether the control button is high or short.

Shape Style Sleek Effect:

<?xml version= "1.0" encoding= "UTF-8"?>

<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >

<solid android:color= ""/>

<stroke android:width= "*DP" android:color= ""/>

<padding android:left= "*DP" android:top= "*DP"

android:right= "*DP" android:bottom= "*DP"/>

<corners android:radius= "*DP"/>

</shape>

CheckBox

Radiogroup

Spinner

Timepicker

ScrollView

ProgressBar

Ratingbar

ImageView

ImageButton android:background= "#00000000"//Set a blank part of the background image to pivot the background directly

Imageswicher&gallery

Gradview

Tab

Menu

Android Development Common layout method learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.