Android Learning Series (ii) layout manager: Linear layout, android linear Layout

Source: Internet
Author: User

Android Learning Series (ii) layout manager: Linear layout, android linear Layout

Reprinted please indicate the source: http://blog.csdn.net/lhy_ycu/article/details/39643669


LinearLayout is a linear layout control in the Android control. Its child controls are arranged horizontally or vertically, arrange all child controls and 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 only has one control or a referenced layout container.



1. Description of attributes of LinearLayout linear layout:

Android: orientation layout direction: "vertical" vertical linear layout, "horizontal" horizontal linear Layout
Android: id specifies the corresponding ID for the control
Android: text specifies the text displayed in the control. Note that the strings. xml file should be used as much as possible here.
Android: gripper specifies the basic position of the control, such as center and right.
Android: textSize specifies the font size of the control.
Android: background specifies the background color used by the control. The RGB naming method is used.
Android: width specifies the width of the control.
Android: height specifies the height of the control.
Android: padding specifies the padding of the control, that is, the content in the control.
Android: If singleLine is set to true, the control content is displayed in the same row.
Android: the default value of layout_weight is 0. The layout_weight attribute can control the relative size of each control in the layout, the linear layout allocates the occupied area for the control based on the ratio of the layout_weight value of the control to the sum of layout_weight values of all controls in the layout.



2. Example of three implementation methods for the LinearLayout Project

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 Second Implementation Method:Code Implementation LinearLayout

<MainActivity. java>

</Pre> <pre name = "code" class = "java">/*** @ author liu * @ description code dynamically creates a linear layout manager */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. main); LinearLayout layout = new LinearLayout (this); // create the current layout manager LinearLayout. layoutParams params = new LayoutParams (ViewGroup. layoutParams. MATCH_PAREN T, ViewGroup. layoutParams. MATCH_PARENT); // set the 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 the component parameter txt. setLayoutParams (txtParams); txt. setText ("Hello! "); Txt. setTextSize (20); layout. addView (txt, txtParams); addContentView (layout, params );}}

2.3 method 3: Customize LinearLayout (inheriting LinearLayout) 2.3.1. Implementation (image rotation)



2.3.2 project structure



2.3.3 detailed coding implementation1) inherit the subclass file MyLinearLayout of LinearLayout. java:

Public class MyLinearLayout extends LinearLayout {/*** view with the name in the xml layout file, which is automatically called by the system during creation. */Public MyLinearLayout (Context context, AttributeSet attrs) {super (context, attrs); initView () ;}/ *** initialize the LinearLayout view */private void initView () {// setOrientation (LinearLayout. VERTICAL); // set the layout parameter LinearLayout. layoutParams params = new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. WRAP_CONTENT); TextView TV = new TextView (getContext (); TV. setText (R. string. hello_world); // Add TextViewaddView (TV, params); for (int I = 0; I <10; I ++) to MyLinearLayout) {ImageView iv = new ImageView (getContext (); iv. setImageResource (R. drawable. ic_launcher); Animation animation1 = AnimationUtils. loadAnimation (getContext (), R. anim. rotate); iv. setAnimation (animation1); // Add 10 animated ImageViewaddView (iv, params) in MyLinearLayout; }}/ *** to layout the child views, determine the location of the sub-view */@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {super. onLayout (changed, l, t, r, B);}/*** callback Method for dimension measurement */@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec );}}

2) The resource file of the master layout, 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) The 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. Rotate Tx and rotate ty indicate the coordinates of the rotation. fillAfter = "true" fillBefore = "false" indicates that the animation is stopped at the last position. fromDegrees toDegrees from 0 ° to 350 ° startOffset: 1 s execution delay duration: animation execution time 3 srepeatCount: Repeated times 3 + 1 --> <rotate android: duration = "3000" android: fromDegrees = "0" android: export Tx = "50% p" android: 0000ty = "20% p" android: repeatCount = "3" android: startOffset = "1000" android: toDegrees = "350" type = "codeph" text = "/codeph"/> </set>

4) main Activity program portal class, MainActivity. java: No need to modify (according to the code automatically generated by Eclipse)

The above are the three basic implementation methods of LinearLayout that I know.


Source code







How to change the default layout of the layout manager in eclipse

Right-click layout, click new, and select android XML file. The default value is Linearlayout.

Android: Linear layout 1. nested linear layout 2 (vertical arrangement). How can I solve this problem if I want to Center 2 vertically in 1?

I have encountered this problem.
If you want to center linear layout 2 in 1, you can closely follow a linear layout 3 in layout 1, and then center layout 2 in layout 3. Write a sample code for you:
<LinearLayout Android: id = "layout 1" layout_width = fill layout_height = fill>
<LinearLayout Android: id = "layout 3" layout_width = fill layout_height = fill gravity = "center">

<LinearLayout id = layout 2 width height ......>
</LinearLayout> <! -- End of 2 -->

</LinearLayout> <! -- End of 3 -->
</LinearLayout> <! -- End of 1 -->

Solve the problem. In layout 3, what should we do. You can use either margin or gravity = center.

Related Article

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.