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

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

WBOY
WBOYOriginal
2016-07-06 13:30:491525browse

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