第21/22讲 UI_布局 之 线性布局

布局管理就是组件在activity中呈现方式,包括组件的大小,间距和对齐方式等。

Android提供了两种布局的实现方式:

1.在xml配置文件中声明:这种方式是将需要呈现的组件在配置文件中进行声明,在程序中通过setContentView

(R.layout.main)方法将视图呈现在activity中通过findViewById()方法获得组件实例。一般推荐这种方式。

2.在程序中通过编码,动态的生成组件以设置相关布局。

Android提供了5种类型的布局类型:

第一个:LinearLayout           (线性布局)

第二个:RelativeLayout         (相对布局)

第三个:TableLayout             (表格布局)

第四个:AbsoluteLayout       (绝对布局)

第五个:FrameLayout           (帧布局)

、LinearLayout
(线性布局)

线性布局,是5种布局最常用的一种,可以将容器里的组件一个挨一个地排列,LinearLayout可以设置各组件的排列方式(横向或者纵向)。

(1) 通过xml配置文件声明

      

1.垂直                                                              2.水平                                                              3.嵌套

android:orientation 控制布局方向,属性值有"vertical"(垂直)和"horizontal"(水平)两种。

android:gravity 控制组件的对齐方式,其值有top,bottom,left,right,center等,默认值为左上角对齐

android:layout_weight 可以对整个视图按比例分割

布局嵌套,这里是一个线性布局里头嵌套另一个线性

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button1"/>

<LinearLayout

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button2" />

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button3"/>

</LinearLayout>

</LinearLayout>

(2)在程序中通过编码设置相关布局

在MainActivity.java中修改:

protected void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(R.layout.main); //设置当前布局的样式。在初建一个activity的时候,程序会帮我们建好

LinearLayout  mLinearLayout =new LinearLayout(this); //创建一个管理对象

/*建立布局样式宽和高,对应xml布局中:android:layout_width="fill_parent"

android:layout_height="fill_parent" */

mLinearLayout.setLayoutParams params= newLinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT,LayoutParams. MATCH_PARENT);

mLinearLayout.setLayoutParams(params);

// 设置方向,对应xml布局中:android:orientation="vertical"

mLinearLayout.setOrientation(LinearLayout.VERTICAL);

TextView mTextView = new TextView(this);      // 创建TextView对象

mTextView.setText("hello world");                       // 设置文字

LinearLayout.LayoutParams mLayoutParams = newLinearLayout.LayoutParams(

LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); // 为其建立布局样式

mLinearLayout.addView(mTextView, mLayoutParams);  // 在父类布局中添加它,及布局样式

}

第21/22讲 UI_布局 之 线性布局的更多相关文章

  1. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  2. android布局之线性布局

    LinearLayout 线性布局有两种,分别是水平线性布局和垂直线性布局,LinearLayout属性中android:orientation为设置线性布局当其="vertical&quo ...

  3. Android 布局(线性布局、相对布局)

    一.线性布局(LinearLayout) <LinearLayout****</LinearLayout>1. orientation(布局方向)value=0 horizontal ...

  4. Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)

    线性布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  5. Android -- UI布局管理,相对布局,线性布局,表格布局,绝对布局,帧布局

    1. 相对布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  6. Android布局之线性布局——LinearLayout

    本文将详细介绍线性布局的各种xml属性. xml属性 <?xml version="1.0" encoding="utf-8"?> <Line ...

  7. 《Tsinghua os mooc》第21~22讲 文件系统

    第二十一讲 文件系统 文件系统是操作系统中管理持久性数据的子系统,提供数据存储和访问功能. 组织.检索.读写访问数据 大多数计算机系统都有文件系统 Google 也是一个文件系统 文件是具有符号名,由 ...

  8. android的布局-----LinearLayout(线性布局)

    学习导图(图片在网上下载) 知识点详解(演示效果方便组件没有设置id) (1)gravity和Layout_gravity android:gravity 属性是对该view中内容的限定.比如一个bu ...

  9. Android开发之线性布局详解(布局权重)

    布局权重 线性布局支持给个别的子视图设定权重,通过android:layout_weight属性.就一个视图在屏幕上占多大的空间而言,这个属性给其设 定了一个重要的值.一个大的权重值,允许它扩大到填充 ...

随机推荐

  1. c#序列化反序列化工具(json,binary,xml)

    using System; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Bina ...

  2. C语言排序算法复习

    排序算法有很多种,这里在复习和分析的基础上,做一个自己的总结: 首先要知道有哪些排序算法,google一下,有云C语言7大经典排序算法(也有8大).主要包括冒泡排序,快速排序,选择排序,插入排序,希尔 ...

  3. Agile 敏捷开发

    简单的说,敏捷开发是一种以人为核心.迭代.循序渐进的开发方法.在敏捷开发中,软件项目的构建被切分成多个子项目,各个子项目的成果都经过测试,具备集成和可运行的特征.换言之,就是把一个大项目分为多个相互联 ...

  4. [Angular 2] Simple intro Http

    To use http, we need to import the HTTP_PROVIDER, so that we can inject http to other component: imp ...

  5. Ajax七层模型用途

    Ajax七层模型 OSI七层模型满足所有网格模型 1.物理层:符合标准: 2.数据链路层:如网卡.水晶头.连接网络层等: 3.网络层:路由器(数据外围打IP地址): 4.传输层:两台计算器端口的连接: ...

  6. Android WiFiDirect 学习(二)——Service Discovery

    Service Discovery 简介 在Android WifiDirect学习(一 )中,简单介绍了如何使用WifiDirect进行搜索——连接——传输. 这样会有一个问题,那就是你会搜索到到附 ...

  7. Intent 能传递的数据类型

    1. Serializable,将对象序列化成二进制数据传递 2. charsequence: 主要用来传递String,char等 3. parcelable: 这个android提供的一种新的类型 ...

  8. ZOJ3555 Ice Climber(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Ice Climber Time Limit: 2 Seconds      Me ...

  9. VC实用小知识总结 (一),转http://blog.csdn.net/myiszjf/article/details/10007431

    在上一篇中,我们以经介绍了程序的流程和框架,在本篇将详细讨论各个功能的实现主要包括 1.获取磁盘信息2.获取目录信息3.获取文件信息4.运行指定文件5.删除指定文件6.删除指定目录7.创建指定目录8. ...

  10. js的一个稍微高级点的用法

    通过问题来说明: 1.一个系统中,要创造很多对象,为每个对象分配一个唯一的ID 1: var createObj = (function(){ 2: var i = 0; 3: return func ...