Android Learning Series (ii) 3 ways to implement the layout manager's linear layout

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 includes child controls that are arranged in a horizontal (horizontal) or vertical (VERTICAL) manner. Arranges all the child controls and the referenced layout containers according to their relative positions.

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 specifying a corresponding ID for a 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. For example, center, right, etc position
ANDROID:TEXTSIZE Specifies the size of the font within 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, which means that the contents of the control
Android:singleline if set to True, the contents of the control are displayed in the same row
The Android:layout_weight default value of 0,layout_weight property controls the relative size of each control in the layout, and the linear layout is based on the control's Layout_weight value and 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.



Two, LinearLayout project demonstration 3 kinds of Realization Way demonstration Example

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 Another way to implement: 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 parameters Layout.setorientation ( linearlayout.vertical); TextView txt = new TextView (this); Linearlayout.layoutparams txtparams = new Layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//Set Component Txt.setlayoutparams (Txtparams); Txt.settext ("hello! (txt.settextsize); Layout.addview (TXT, txtparams); Addcontentview (layout, params);}}

2.3 The Third way to implement : Define your own implementation linearlayout (inherit LinearLayout) 2.3.1, implementation (picture rotation)



2.3.2, Project structure chart



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

public class Mylinearlayout extends LinearLayout {/** * in the XML layout file is the famous view, created by the system itself to invoke. */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 to 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) to the 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 size */@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 run duration: Animation run time 3srepeatCount: 3+1 repeated times---    <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 changes required (as per Eclipse's own active code)

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


Source






Android Learning Series (ii) 3 ways to implement the layout manager's linear layout

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.