[Android] Chapter 3 (3) LinearLayout (linear layout) and linearlayout

Source: Internet
Author: User

[Android] Chapter 3 (3) LinearLayout (linear layout) and linearlayout

Category: C #, Android, VS2015;

Created on:

I. Introduction

LinearLayout stacks components in the container one by one horizontally or vertically (without overlap ). This layout is very similar to the StackPanel control of WPF. It also sets whether the direction of the layout is vertical or horizontal through the orientation attribute ).

Common attributes include:

Android: layout_gravity: Alignment of child elements in the container. That is, to which side is sunk (gravity: gravity ).

Android: orientation: controls the arrangement of child elements (horizontal and vertical)

Android: layout_weight: The allocation weight of the space occupied by its child elements. The smaller the value, the larger the weight.

For example:

<LinearLayout

Android: orientation = "horizontal"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: layout_weight = "1"

Note: The width and height of sub-elements must have at least one linear layout set to "fill_parent.

Ii. Example-Demo01LinearLayout

1. Design Interface

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android =" http://schemas.android.com/apk/res/android "Android: orientation =" vertical "android: layout_width =" match_parent "android: layout_height =" match_parent "> <LinearLayout android: orientation =" horizontal "android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1"> <TextView android: text = "red" android: gravity = "center_horizontal" android: background = "# aa0000" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "green" android: gravity = "center_horizontal" android: background = "#00aa00" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "blue" android: gravity = "center_horizontal" android: background = "# tianaa" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "yellow" android: gravity = "center_horizontal" android: background = "# aaaa00" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> </LinearLayout> <LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1"> <TextView android: text = "Row 1st" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "row 2nd" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "Row 3rd" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "Row 4th" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> </LinearLayout>

4. Add the Demo01LinearLayout. cs file.

Add the file in the SrcDemos folder.

using Android.App;using Android.OS;namespace ch07demos.SrcDemos{    [Activity(Label = "Demo01LinearLayout")]    public class Demo01LinearLayout : Activity    {        protected override void OnCreate(Bundle savedInstanceState)        {            base.OnCreate(savedInstanceState);            SetContentView(Resource.Layout.Demo01LinearLayout);        }    }}

Observe the running effect.

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.