search
Homephp教程php手册[Android] Chapter 7 (3) LinearLayout (Linear Layout)

Category: C#, Android, VS2015; Creation date: 2016-02-10 1. Introduction LinearLayout stacks the components in the container one by one horizontally or vertically (without overlapping). The function of this layout is very similar to that of WPF's StackPanel control. It also uses the orientation attribute to set the direction of arrangement whether vertical or vertical (ho

Category: C#, Android, VS2015;

Creation date: 2016-02-10

1. Introduction

LinearLayout stacks the components in the container one by one horizontally or vertically (without overlapping). The function of this layout is very similar to that of WPF's StackPanel control. The orientation property is used to set the arrangement direction whether vertical or horizontal.

Commonly used attributes include:

android:layout_gravity: The alignment of child elements in the container. That is: which end it sinks to (gravity: gravity).

android:orientation: Control the arrangement of child elements (horizontal arrangement, vertical arrangement)

android:layout_weight: Indicates the allocation weight of the space occupied by its child elements. The smaller the value, the greater the weight.

For example:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

Note: At least one of the width and height of the child element must be set to "fill_parent" for linear layout to work.

2. Example—Demo01LinearLayout

1. Design interface

[Android] Chapter 7 (3) LinearLayout (Linear Layout)

2. Operation effect

[Android] Chapter 7 (3) LinearLayout (Linear Layout)

3. Add the Demo01LinearLayout.axml file

Add this file under the layout folder.

<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span>
<span style="color: #0000ff;"><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;">
    android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;">
    android:layout_width</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;">
    android:layout_height</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #0000ff;">></span>
  <span style="color: #0000ff;"><span style="color: #800000;">LinearLayout
      </span><span style="color: #ff0000;">android:orientation</span><span style="color: #0000ff;">="horizontal"</span><span style="color: #ff0000;">
      android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
      android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
      android:layout_weight</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="red"</span><span style="color: #ff0000;">
        android:gravity</span><span style="color: #0000ff;">="center_horizontal"</span><span style="color: #ff0000;">
        android:background</span><span style="color: #0000ff;">="#aa0000"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="green"</span><span style="color: #ff0000;">
        android:gravity</span><span style="color: #0000ff;">="center_horizontal"</span><span style="color: #ff0000;">
        android:background</span><span style="color: #0000ff;">="#00aa00"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="blue"</span><span style="color: #ff0000;">
        android:gravity</span><span style="color: #0000ff;">="center_horizontal"</span><span style="color: #ff0000;">
        android:background</span><span style="color: #0000ff;">="#0000aa"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="yellow"</span><span style="color: #ff0000;">
        android:gravity</span><span style="color: #0000ff;">="center_horizontal"</span><span style="color: #ff0000;">
        android:background</span><span style="color: #0000ff;">="#aaaa00"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
  <span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span>
  <span style="color: #0000ff;"><span style="color: #800000;">LinearLayout
      </span><span style="color: #ff0000;">android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;">
      android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
      android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
      android:layout_weight</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="第1行"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="15pt"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="第2行"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="15pt"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="第3行"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="15pt"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView
        </span><span style="color: #ff0000;">android:text</span><span style="color: #0000ff;">="第4行"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="15pt"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_weight</span><span style="color: #0000ff;">="1"</span> <span style="color: #0000ff;">/></span>
  <span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span>

4. Add the Demo01LinearLayout.cs file

Add this file under the SrcDemos folder.

<span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS;
</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> ch07demos.SrcDemos
{
    [Activity(Label </span>= <span style="color: #800000;">"</span><span style="color: #800000;">Demo01LinearLayout</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo01LinearLayout : Activity
    {
        </span><span style="color: #0000ff;">PRotected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle savedInstanceState)
        {
            </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Demo01LinearLayout);
        }
    }
}</span>

Run and observe the effect.


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)