Android Learning Series (ii) Layout manager's linear layout and its custom implementation

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39643669


LinearLayout is a linear layout control in an Android control that contains child controls that are arranged in a horizontal (horizontal) or vertical (VERTICAL) manner. Arranges all child controls and the referenced layout container by relative position. When the boundary is exceeded, some controls are missing or disappear. Therefore, each row of a vertical list will have only one control or a referenced layout container.



LinearLayout description of the relevant properties of the linear layout:

Android:orientation Layout Direction: "Vertical" vertical linear layout, "horizontal" horizontal line layout
Android:id specify the appropriate ID for the control
ANDROID:TEXT Specifies the text that is displayed in the control, it is important to note that the characters in the Strings.xml file are used here as much as possible
Android:grivity the base position of the specified control, such as center, right, etc position
ANDROID:TEXTSIZE Specifies the size of the font in the control
ANDROID:BACKGROUND Specifies the background color used by the control, and the RGB naming method
Android:width the width of the specified control
Android:height the height of the specified control
android:padding Specifies the padding for the control, meaning that the contents of the control
Android:singleline if set to True, the contents of the control are displayed in the same profession
The android:layout_weight default is the 0,layout_weight property to control the relative size of the individual controls in the layout, and the linear layout will layout_weight the value of the control with its·The ratio of the sum of the layout_weight values for all controls in the layout is assigned to the area occupied by the control.



Ii. LinearLayout Project Demonstration 3 Examples of implementation methods

2.1 First Implementation method: XML Configuration Implementation LinearLayout <activity_main.xml>

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >    <textview        android:layout_width= "Match_parent"        android: layout_height= "Wrap_content"        android:text= "hello!        " Android:textsize= "20SP"/></linearlayout>


2.2 The second implementation method: code implementation LinearLayout

<MainActivity.java>

</pre><pre name= "code" class= "java" >/** * @author Liu * @description code dynamically create a linear layout manager */public class Mainactivit Y extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); /Setcontentview (R.layout.main); LinearLayout layout = new LinearLayout (this);//Create the current layouts manager linearlayout.layoutparams params = new Layoutparams ( viewgroup.layoutparams.match_parent,viewgroup.layoutparams.match_parent);//Set Linear layout parameter layout.setorientation ( linearlayout.vertical); TextView txt = new TextView (this); Linearlayout.layoutparams txtparams = new Layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//Set component parameter Txt.setlayoutparams (txtparams); Txt.settext ("hello! (txt.settextsize); Layout.addview (TXT, txtparams); Addcontentview (layout, params);}}

2.3 Third Implementation : Custom implementation linearlayout (Inherit LinearLayout) 2.3.1, implementation (picture rotation)



2.3.2, Project structure chart



2.3.3, Detailed coding implementation1) Inherit linearlayout subclass file Mylinearlayout.java:

public class Mylinearlayout extends LinearLayout {/** * The view that is known in the XML layout file is automatically called by the system when it is created. */public Mylinearlayout (Context context, AttributeSet Attrs) {Super (context, attrs); Initview ();} /** * Initialize linearlayout view */private void Initview () {//Set LinearLayout layout direction setorientation (linearlayout.vertical);// Set layout parameters Linearlayout.layoutparams params = new Layoutparams (layoutparams.match_parent, layoutparams.wrap_content); TextView TV = new TextView (GetContext ()); Tv.settext (R.string.hello_world);//Add mylinearlayout inside Textviewaddview (TV, params); for (int i = 0; i < i++) {ImageView IV = new ImageView (GetContext ()); Iv.setimageresource (R.drawable.ic_laun Cher); Animation Animation1 = animationutils.loadanimation (GetContext (), r.anim.rotate); Iv.setanimation (animation1);// Add 10 Imageviewaddview (iv, params) in Mylinearlayout;}} /** * Layout of sub-view to determine the location of child view */@Overrideprotected void OnLayout (Boolean changed, int l, int t, int r, int b) {super.onlayou T (changed, L, T, R, b);} /** * Callback method when measuring dimensions */@Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, Heightmeasurespec);}} 

2) Main layout resource file, Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical" >    <com.liu.mylinearlayout01.mylinearlayout        android:layout_ Width= "Match_parent"        android:layout_height= "Match_parent"/></linearlayout>

3) Animation file Rotate.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:fillafter= "true"    android:fillbefore= "false" >    <!--    rotation effect, pivotx,pivoty refers to rotation coordinates;    Fillafter= "true" fillbefore= "false" means that the animation stops at the last position;   Fromdegrees todegrees from 0 ° to 350° startoffset: delay 1s execution duration: Animation execution time 3srepeatCount: Repeat 3+1-    -    <rotate        android:duration= "3000"        android:fromdegrees= "0"        android:pivotx= "50%p"        android:pivoty= "20%p"        android:repeatcount= "3 "        android:startoffset=" "        android:todegrees="/></set>

4) , main activity program Ingress class, Mainactivity.java: No modifications required (by Eclipse Auto-generated code)

The above is the author know the linearlayout of the three basic ways to achieve.


Source






Android Learning Series (ii) Layout manager's linear layout and its custom implementation

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.