概述
什么是线性布局
线性布局是最简单,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定义如下:
<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对象放入其中:
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)
最后
以上就是聪慧篮球为你收集整理的安卓课程八 线性布局的全部内容,希望文章能够帮你解决安卓课程八 线性布局所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复