爱黑武论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4749|回复: 6

[安卓技术] Android开发——Linear Layout

[复制链接]
发表于 2011-1-23 16:32 | 显示全部楼层 |阅读模式

立即注册,加入爱黑武论坛的大家庭!爱黑武,爱上搞机生活!

您需要 登录 才可以下载或查看,没有账号?注册

x
这个是Android Docs里面的例子。
我只是把我理解的写出来,说的不好,欢迎指出。
Linear Layout继承自ViewGroup,它只有两种模式:vertically 和 horizontally(垂直排列和水平排列)。

首先建立一个Android项目:
然后打开:res/layout/main.xml
main.xml已经说过了,是一个布局文件。
把内容修改成:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">

  6.   <LinearLayout
  7.       android:orientation="horizontal"
  8.       android:layout_width="fill_parent"
  9.       android:layout_height="fill_parent"
  10.       android:layout_weight="1">
  11.       <TextView
  12.           android:text="red"
  13.           android:gravity="center_horizontal"
  14.           android:background="#aa0000"
  15.           android:layout_width="wrap_content"
  16.           android:layout_height="fill_parent"
  17.           android:layout_weight="1"/>
  18.       <TextView
  19.           android:text="green"
  20.           android:gravity="center_horizontal"
  21.           android:background="#00aa00"
  22.           android:layout_width="wrap_content"
  23.           android:layout_height="fill_parent"
  24.           android:layout_weight="1"/>
  25.       <TextView
  26.           android:text="blue"
  27.           android:gravity="center_horizontal"
  28.           android:background="#0000aa"
  29.           android:layout_width="wrap_content"
  30.           android:layout_height="fill_parent"
  31.           android:layout_weight="1"/>
  32.       <TextView
  33.           android:text="yellow"
  34.           android:gravity="center_horizontal"
  35.           android:background="#aaaa00"
  36.           android:layout_width="wrap_content"
  37.           android:layout_height="fill_parent"
  38.           android:layout_weight="1"/>
  39.   </LinearLayout>
  40.         
  41.   <LinearLayout
  42.     android:orientation="vertical"
  43.     android:layout_width="fill_parent"
  44.     android:layout_height="fill_parent"
  45.     android:layout_weight="1">
  46.     <TextView
  47.         android:text="row one"
  48.         android:textSize="15pt"
  49.         android:layout_width="fill_parent"
  50.         android:layout_height="wrap_content"
  51.         android:layout_weight="1"/>
  52.     <TextView
  53.         android:text="row two"
  54.         android:textSize="15pt"
  55.         android:layout_width="fill_parent"
  56.         android:layout_height="wrap_content"
  57.         android:layout_weight="1"/>
  58.     <TextView
  59.         android:text="row three"
  60.         android:textSize="15pt"
  61.         android:layout_width="fill_parent"
  62.         android:layout_height="wrap_content"
  63.         android:layout_weight="1"/>
  64.     <TextView
  65.         android:text="row four"
  66.         android:textSize="15pt"
  67.         android:layout_width="fill_parent"
  68.         android:layout_height="wrap_content"
  69.         android:layout_weight="1"/>
  70.   </LinearLayout>

  71. </LinearLayout>
复制代码
在Graphical Layout界面可以看到效果:
截图00.png
粗略地解释其内容:
首先用一个LinearLayout
填充界面: android:layout_width="fill_parent"
                   android:layout_height="fill_parent",
垂直排序:android:orientation="vertical"。

然后在这个LinearLayout里面填充两个LinearLayout,一个水平排序(里面放三个显示控件),一个垂直排序(放四个显示控件)
android:gravity是组件内部的对齐方式
android:layout_weight的默认值是0,意味着他们只占据它们需要显示的空间大小。
如果不为0的话,就按比例分配空间。
android:background背景颜色;
android:text显示的内容;
android:textSize字体大小;
wrap_content是适当大小,fill_parent占满父视图的空间。

而MainActivity.java的onCreate()里面则加上一句代码:
  1.     public void onCreate(Bundle savedInstanceState) {
  2.         super.onCreate(savedInstanceState);
  3.         setContentView(R.layout.main);
  4.         this.setTitle("Linear Layout练习");
  5.     }
复制代码
试运行一下效果:
截图01.png
当然,在编辑main.xml时,应该好好练习一下怎样才能快速地编辑。
很多属性都不用自己输入的,在graphical layou界面都可以完成的(通过拖控件等操作)。
当然,一些布局还是要自己去去修改main.xml。
发表于 2011-1-23 16:52 | 显示全部楼层
这个对我这种门外汉真是一头雾水
发表于 2011-1-23 17:50 | 显示全部楼层
共同学习。呵呵!!!
发表于 2011-1-23 18:16 | 显示全部楼层
过来支持一下~ 瞧瞧~ [s:61]
发表于 2011-1-23 18:53 | 显示全部楼层
支持一下下。。。
发表于 2011-1-23 19:26 | 显示全部楼层
看不懂,悲剧!
发表于 2013-8-8 09:26 | 显示全部楼层
自学很不错了~~~
回复 支持 反对

使用道具 举报

 Hello,黑武的好机友!回复想偷个懒?点这里: 
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|Archiver|手机版|爱黑武论坛 ( 京ICP备2023028323号 | 京公网安备11011202000270号 )

GMT+8, 2024-6-2 01:31 , Processed in 0.040191 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2024, ihei5.com

快速回复 返回顶部 返回列表