Home > php教程 > php手册 > body text

【Android】第7章(3)LinearLayout(线性布局)

WBOY
Release: 2016-07-06 13:30:49
Original
1446 people have browsed it

分类:C#、Android、VS2015; 创建日期:2016-02-10 一、简介 LinearLayout将容器内的组件一个挨着一个地横向或纵向依次堆叠起来(不重叠)。该布局和WPF的StackPanel控件的功能非常相似,也是通过orientation属性设置排列的方向是纵向(vertical)还是纵向(ho

分类:C#、Android、VS2015;

创建日期:2016-02-10

一、简介

LinearLayout将容器内的组件一个挨着一个地横向或纵向依次堆叠起来(不重叠)。该布局和WPF的StackPanel控件的功能非常相似,也是通过orientation属性设置排列的方向是纵向(vertical)还是纵向(horizontal)。

常用属性有:

android:layout_gravity:子元素在容器中的对齐方式。即:往哪一端偏沉(gravity:重力)。

android:orientation:控制子元素的排列方式(横向排列、纵向排列)

android:layout_weight:表示其子元素占用空间的分配权重,值越小权重越大。

例如:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

注意:子元素的宽和高必须至少有一个设置为“fill_parent”线性布局才会起作用。

二、示例—Demo01LinearLayout

1、设计界面

image

2、运行效果

image

 

3、添加Demo01LinearLayout.axml文件

在layout文件夹下添加该文件。

<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>
Copy after login

4、添加Demo01LinearLayout.cs文件

在SrcDemos文件夹下添加该文件。

<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>
Copy after login

运行观察效果。


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!