第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. 解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题

    Xcode把应用程序打包成ipa是本文要介绍的内容,不多说,先俩看内容.注意:本方法需要先制作假凭证编译于项目中,否则产生的ipa还是无法于iPhone中运行. 制作方法请参考: http://blo ...

  2. setNeedsDisplay setNeedsLayout

    setNeedsDisplay调用drawRect方法来实现view的绘制,而setNeedsLayout则调用layoutSubView来实现view中subView的重新布局 转自  http:/ ...

  3. Java 泛型数组

    Java 不支持泛型数组.也就是说, List<String>[] ls = new ArrayList<String>[10]; 是不支持的,而 List<String ...

  4. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  5. Linux(CentOS 5.5) Redis安装

    一,什么是redis redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset ...

  6. Android 中文 API (40) —— RatingBar

    Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...

  7. android View各属性详解

    一.有8个直接子类:AnalogClock, ImageView, KeyboardView, ProgressBar, SurfaceView, TextView, ViewGroup, ViewS ...

  8. Linux下一些基本操作

    一.忘记root密码 1. sudo passwd root 2. 输入新密码. 二.查看内核版本: 1.查看内核版本命令:1) cat /proc/version 2) uname -a 3)  u ...

  9. Java基础知识强化61:经典查找之 常见查找算法小结

    一.顺序查找 条件:无序或有序队列. 原理:按顺序比较每个元素,直到找到关键字为止. 时间复杂度:O(n) 二.二分查找(折半查找) 条件:有序数组 原理:查找过程从数组的中间元素开始,如果中间元素正 ...

  10. Dev系列控件的AJAX (转)

    介绍Dev系列控件在前台也就是客户端的一些常用方法介绍以及前后台异步通信的方法. 一.Dev Data Edit控件通用属性以及方法: 属性 1.GetEnabled():返回控件是否为可操作状态 2 ...