1.LinearLayout类简介
线性布局是最简单的布局之一,它提供了控件水平或者垂直排列的模型。同时,使用此布局时可以通过设置控件的weight参数控制各个控件在容器中的相对大小。LinearLayout布局的属性即可以在布局文件(XML)中设置,也可以通过成员方法进行设置。下表给出了LinearLayout常用的属性及这些属性的对应设置方法。
属性名称 | 对应方法 | 描述 |
android:orientation | setOrientation(int) | 设置线性布局的朝向,可取horizontal和vertical两种排列方式 |
android:gravity | setGravity(int) | 设置线性布局的内部元素的布局方式 |
在线性布局中可使用gravity属性来设置控件的对齐方式,gravity可取的值及说明如下表所示。
属性值 | 说明 |
top | 不改变控件大小,对齐到容器顶部 |
bottom | 不改变控件大小,对齐到容器底部 |
left | 不改变控件大小,对齐到容器左侧 |
right | 不改变控件大小,对齐到容器右侧 |
center_vertical | 不改变控件大小,对齐到容器纵向中央位置 |
center_horizontal | 不改变控件大小,对齐到容器横向中央位置 |
center | 不改变控件大小,对齐到容器中央位置 |
fill_vertical | 若有可能,纵向拉伸以填满容器 |
fill_horizontal | 若有可能,横向拉伸以填满容器 |
fill | 若有可能,纵向横向同时拉伸以填满容器 |
2.线性布局案例
通过一个案例来说明LinearLayout的用法。本案例的开发步骤如下。
1)新建一个项目Android_Sample_3_1。
2)首先打开项目文件夹下res/values目录下的strings.xml,在其中输入如下代码。
1: <?xml version="1.0" encoding="utf-8"?>
2: <resources>
3: <string name="app_name">LinearExample</string>
4: <string name="button">按钮</string>
5: <string name="add">添加</string>
6: </resources>
在strings.xml中主要声明了程序中要用到的字符串资源,这样将所有字符串资源统一管理有助于提高程序的可读性及可维护性。
3)打开项目文件夹下的res/layout目录下的main.xml,将其中已有的代码替换为如下代码。
1: <?xml version="1.0" encoding="utf-8"?>
2: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3: android:id="@+id/lla"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent"
6: android:gravity="right"
7: android:orientation="horizontal">
8:
9: <Button
10: android:id="@+id/Button01"
11: android:layout_width="wrap_content"
12: android:layout_height="wrap_content"
13: android:text="@string/add" >
14: </Button>
15:
16: </LinearLayout>
第2~7行申明了一个线性布局,第7行设置线性布局的朝向为垂直排列。
第4~5行设置该线性布局在其所属的父容器中的布局方式为横向和纵向填充父容器。
第3行为该线性布局声明了ID。
第6行设置该线性布局内部元素的布置方式为向右对齐。
第9~14行声明了一个Button控件,其ID为Button01,第13行设置Button控件显示的文本内容为资源文件strings.xml中的属性值。
第11~12行行设置Button控件在父容器中的布局方式为只占据自身大小的空间。
3)打开项目的Activity文件Android_Sample_3_1Activity.java,将其中已有的代码替换为如下的代码。
1: package wyf.jc;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.view.View;
6: import android.widget.Button;
7: import android.widget.LinearLayout;
8:
9: public class Android_Sample_3_1Activity extends Activity {
10: int count = 0;
11:
12: @Override
13: public void onCreate(Bundle savedInstanceState) {
14: super.onCreate(savedInstanceState);
15: setContentView(R.layout.main);
16: Button button = (Button) findViewById(R.id.Button01);// 获取屏幕中的按钮控件对象
17: button.setOnClickListener(new View.OnClickListener() {
18:
19: @Override
20: public void onClick(View v) {
21: LinearLayout ll = (LinearLayout) findViewById(R.id.lla);// 获取线性布局对象
22:
23: String msg = Android_Sample_3_1Activity.this.getResources()
24: .getString(R.string.button);
25:
26: Button tempbutton = new Button(Android_Sample_3_1Activity.this);// 创建一个Button对象
27: tempbutton.setText(msg + (++count));// 设置Button控件显示的内容
28: tempbutton.setWidth(80);// 设置Button控件的宽度
29: ll.addView(tempbutton);// 向线性布局中添加View
30:
31: }
32: });
33:
34: }
35: }
第10行声明了用于记录生成的按钮编号的计数器。
第16行通过findViewById方法获取屏幕中的Button控件对象。
第17~32行为对OnClickListener接口中onClick方法的实现,在该方法中首先获得线性布局LinearLayout对象的引用,然后创建一个Button对象并调用LinearLayout对象的addView方法将其添加到线性布局容器中。
4)完成上述步骤的工作后,运行项目。
在程序中单击“添加”按钮可向屏幕中添加新按钮,效果如下图所示。
将步骤3中第7行代码改为如下代码。
1: android:orientation="horizontal"
重新运行项目Android_Sample_3_1,在程序中可以单击”添加“按钮向屏幕中添加新按钮,如下图所示。
提示:在线性布局中垂直分布时占一列,水平分布时占一行。特别要注意的是,水平或垂直分布时如果超过一行,则不会像Java SE中的FlowLayout那样自动换行或换列,超出屏幕的子控件将不会被显示,除非将其放到ScrollView中。
作者:银月莲
出处:http://www.cnblogs.com/moonsilvering
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,包括文章,代码,图片等本站内所有资源,否则保留追究法律责任的权利。