新知识介绍:
layout:布局。我们有很多控件,但是我们要把这些控件如何摆放,怎么样摆放合理、美观,这就是布局了。
有以下几种布局方式:FrameLayout、LinearLayout 、RelativeLayout 、TableLayout 。
现在我们来介绍前两种:
1、FrameLayout:是 最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定 在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡 住(除非后一个子元素是透明的)。
FrameLayout默认填充widget等在左上角,然后后面的控件遮住前面的,比如说有两个TextView,Text内容分别是I am textview 1 和I am textview 2 看到的效果会如下(只显示最新的):
|
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="htHTMLtp://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"
/>
</FrameLayout>
|
这个布局并不常用,大家可以看看就行了。
1、LinearLayout:单向布局,最常见的布局,可以有横向或者纵向布局。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有一个元素,而不管他们有多 宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以及互相对齐(相对一个元素 的右对齐、中间对齐或者左对齐)。
LinearLayout 还支持为单独的子元素指定weight。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个weight值,剩余的空间就会按这些子元素指定的weight比例分配给这些子元素。默认的weight值为0。例如,如 果有三个文本框,其中两个指定了weight值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。
LinearLayout是Android sdk创建project时的默认Layout,支持两种方式,默认是垂直vertical的线性layout,另一个是水平horizontal方向的线性排列。
话不多说,看代码:
这个是横向排列,"vertical":
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 1"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I am textview 2"
/>
</LinearLayout>
|
这个是纵向排列,"horizontal":
|
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="10px"
android:layout_height="fill_parent"
android:text="I am textview 1"
/>
<TextView
android:layout_width="10px"
android:layout_height="wrap_content"
android:text="I am textview 2"
/>
</LinearLayout>
|
怎么样,现在清楚了吧? 至于weight,大家可以下去调试,下节课我将讲解。
我上传的附件是横向和纵向相结合的布局,可以看看。
搞定,打完收工!
分享到:
相关推荐
Android有Layout:FrameLayout,LinearLayout,TableLayout,RelativeLayout,AbsoluteLayout。 放入Layout中进行排布的View的XML属性: 几种Layout中Item所共有的XML属性: (1)layout_width (2)layout_height ...
五种布局 framelayout linearlayout relativelayout tablelayout tablayout 两个控件 gridview listview实例代码,可直接运行
android:layout_height="wrap_content" android:text="FrameLayout"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Frame Layout"> </...
了解布局Layout的应用 多种Layout: AbsoluteLayout FrameLayout GridView LinearLayout ListLayout RadioGroup TableLayout ……… 3. 利用布局安排各种控件,设计良好用户界面 "<LinearLayout " "xmlns:android=...
Android 开发 (实验五) 实验题目:... 二、实验内容 组 件 布 局 有 : LinearLayout 、 TableLayout 、 FrameLayout 、 RelativeLayout 三、实验步骤 1、用表格布局完成登录界面 <TableLayout xmlns:android=...
PercentClipView :star: Cropped layouts with percentage for ...LinearLayout layout = (LinearLayout) this.findViewById(R.id.layout); layout.setClipLeft(0.50f); Reference Class FrameLayout LinearLayo
了解布局Layout的应用 多种Layout: AbsoluteLayout FrameLayout GridView LinearLayout ListLayout RadioGroup TableLayout ……… 3. 利用布局安排各种控件,设计良好用户界面 "<LinearLayout " "xmlns:android=...
Android Layout多种使用方法源码演示,这个例子中演示了如何使用FrameLayout、如何使用RelativeLayout、如何使用RelativeLayout和LinearLayout以及如何使用TableLayout的例子,都挺实用,平时用的比较多,但是比较...
第1步:新建一个Android工程ApplicationDemo,目录结构如下: 第2步:新建一个工具类ToolsUtil.java,代码如下 package com.tutor.application; import android.content.Context; import android.widget.Toast;...
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/layout_main_top" /> <android.support.v4.view.ViewPager ...
《Android高薪之路:Android程序员面试宝典》目录: 第1章 Android入门 1 1 关于Android的非技术问题 1 1 1 为什么看好 Android 1 1 2 以前是否从事过Android的工作 做过哪些工作 1 1 3 你做的最复杂的界面是什么 1 ...
线性布局LinearLayout 框架布局FrameLayOut RelativeLayout相对布局 绝对布局AbsoluteLayout TableLayout表格布局
布局管理器 1. FrameLayout:帧布局。 所有的子组件默认是从布局左上角层叠添加进来。后添加的组件会在前一组件之上进行覆盖填充。 2. LinearLayout:线性布局。设置垂直或水平属性值来排列每个子组件。
浅谈Android五大布局 ——LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout
android:layout_width="match_parent" android:layout_height="match_parent" dragger_layout:drag_view_id="@ id/drag_view" dragger_layout:shadow_view_id="@ id/shadow_view" dragger_layout:drag...
包括LinearLayout、RelativeLayout、TableLayout、FrameLayout、AbsoluteLayout等常见layout的示例程序
在Android程序中界面是通过布局文件设定的,在每个应用程序创建时会默认包含一个主界面布局,该布局位于res/layout目录中。 实际开发中每个应用程序都包含多个界面,而程序默认提供的一个主界面布局无法满足需求,...
android最全的布局属性总结,各种Layout如linearlayout ,relativeLayout,frameLayout,各种widget属性说明