我是靠谱客的博主 聪慧篮球,这篇文章主要介绍安卓课程八 线性布局,现在分享给大家,希望可以做个参考。

什么是线性布局

    线性布局是最简单,Android开发者使用得最多的布局类型之一,开发者用它来组织你们的用户界面上的控件。线性布局的作用就像它的名字一样:它将控件组织在一个垂直或水平的形式。当布局方向设置为垂直时,它里面的所有子控件被组织在同一列中;当布局方向设置为水平时,所有子控件被组织在一行中。

    线性布局可以在XML布局资源文件中定义,也可以用Java代码在程序中动态的定义。

常用属性:

layout_width布局的宽度
layout_height布局的高度
orientation布局的方向(横向,纵向显示)

  下面展示了一个包含7个TextView控件的线性布局。这个线性布局方向被设置为垂直,导致每个TextView控件被显示在一列当中。每一个TextView控件的文本属性都是一个颜色值,背景色就是这个颜色;通过将控件的layout_width属性设置为fill_parent,每个控件都拉伸到屏幕宽度.

   用XML布局资源定义线性布局

  设计程序用户界面最方便和可维护的方法是创建XML布局资源。这个方法极大地简化了UI设计过程,它将很多静态创建和用户界面控件的布局以及控件属性的定义移到了XML中,而不是写代码。

       XML布局资源必须被存储在项目目录的/res/layout下。让我们看看彩虹线性布局。这个屏幕基本上就是一个设置为铺满整个屏幕的垂直线性布局,这通过设置它的layout_width和layout_height属性为fill_parent来实现。适当地将其命名为/res/layout/rainbow.xml,XML定义如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:text="@string/red" android:id="@+id/TextView01" android:layout_height="wrap_content" android:background="#f00" android:layout_width="fill_parent" android:layout_weight=".14" android:gravity="center" android:textColor="#000"> </TextView> <TextView android:text="@string/orange" android:id="@+id/TextView02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".15" android:background="#ffa500" android:gravity="center" android:textColor="#000"> </TextView> <TextView android:text="@string/YELLOW" android:id="@+id/TextView03" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".14" android:background="#ffff00" android:gravity="center" android:textColor="#000"> </TextView> <TextView android:text="@string/GREEN" android:id="@+id/TextView04" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".15" android:background="#0f0" android:gravity="center" android:textColor="#000"> </TextView> <TextView android:text="@string/BLUE" android:id="@+id/TextView05" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".14" android:background="#00f" android:gravity="center" android:textColor="#fff"> </TextView> <TextView android:text="@string/INDIGO" android:id="@+id/TextView06" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".14" android:background="#4b0082" android:gravity="center" android:textColor="#fff"> </TextView> <TextView android:text="@string/VIOLET" android:id="@+id/TextView07" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight=".14" android:background="#ee82ee" android:gravity="center" android:textColor="#000"> </TextView> </LinearLayout>

 用程序动态定义线性布局

你也可以通过程序来创建和配置线性布局。这通过LinearLayout(android.widget.LinearLayout)类来实现。你能在LinearLayout.LayoutParams类中找到子级细节。同样地,典型的布局参数(android.view.ViewGroup.LayoutParams),如layout_height和layout_width, 以及边距参数(ViewGroup.MarginLayoutParams)也能用在LinearLayout对象上。

下面的代码示例了如何用程序在Activity中实例化一个线性布局并在它的onCreate()方法中将三个TextView对象放入其中:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
TextView tv1 = new TextView(this); tv1.setText("FIRST"); tv1.setTextSize(20); tv1.setGravity(Gravity.CENTER); TextView tv2 = new TextView(this); tv2.setTextSize(20); tv2.setGravity(Gravity.CENTER); tv2.setText("MIDDLE"); TextView tv3 = new TextView(this); tv3.setTextSize(20); tv3.setGravity(Gravity.CENTER); tv3.setText("LAST"); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ll.setGravity(Gravity.CENTER); ll.addView(tv1); ll.addView(tv2); ll.addView(tv3); setContentView(ll);

 探讨线性布局的重要特性和属性

现在让我们来看看有助于配置线性布局和它的子控件的一些重要属性。
 
 一些特别的属性应用到线性布局。你会使用到线性布局最重要的属性包括:
 •方向属性(必须),android:orientation="vertical" 取值可以是vertical或horizontal(类:LinearLayout)
 •对齐属性(可选),android:layout_gravity,控制子控件在线性布局中如何排列和显示(类:LinearLayout)
 •layout_weight属性(可选,应用到每个子控件)指定每个子控件在父级线性布局中的相对重要性(类:LinearLayout.LayoutParams)
 
  此外,通用的ViewGroup-style属性也应用到线性布局。这些属性包括:
 •通用布局参数如layout_height (必须)和layout_width (必须) (类:ViewGroup.LayoutParams)
 •边距布局参数如margin_top,margin_left,margin_right和margin_bottom (类:ViewGroup. MarginLayoutParams)
 •布局参数如layout_height和layout_width (类:ViewGroup.LayoutParams)

最后

以上就是聪慧篮球最近收集整理的关于安卓课程八 线性布局的全部内容,更多相关安卓课程八内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部