第一种布局: LinearLayout

    LinearLayout又称作线性布局,是一种非常常用的布局。正如它名字所描述的一样,这 个布局会将它所包含的控件在线性方向上依次排列。    既然是线性排列,肯定就不仅只有一个方向。我们通过 android:orientation属性指定了排列方向。

1. Linearlayout的android:orientation属性

1.1 “vertical” 垂直分布

    当设置成vertical时,不能设置android:layoy_height属性设置为match_parent,否则其他控件都将被覆盖

1.2 “horizontal” 水平分布 ——默认值

    当设置成horizontal时,不能设置android:layoy_width属性设置为match_parent,否则其他控件都将被覆盖

2. 在LinearLayout的布局中,控件与布局相关的属性

2.1 android:gravity 指文字在控件中的对齐方式

2.2 android:layout_gravity 指控件在布局中的对其方式

    layout_gravity的值有三种:bottom、top、center_vertical    需要注意,当 LinearLayout的排列方向是 horizontal时,只有垂直方向上的对齐方式才会生效,因为此时水 平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。    同样的道理,当 LinearLayout的排列方向是 vertical时,只有水平方 向上的对齐方式才会生效。

xml文件中的代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >      <Button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="top"         android:text="Button 1" />     <Button         android:id="@+id/button2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_vertical"         android:text="Button 2" />     <Button         android:id="@+id/button3"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="bottom"         android:text="Button 3" />  LinearLayout>

当布局为水平布局时,空间选择在布局文件中的对齐方式分别为上中下对齐的效果效果图:
详解android四种布局之LinearLayout_第1张图片

2.3 android:layout_weight 这个属性允许我们使用比例的方式来指定控件的大小

    当设置了layout_weight属性后,layout_width/layout_height属性的值若为数字将失效(其余都覆盖layout_weight属性),一般设置了layout_weight的属性后,若是水平布局,控件的高度不可设置为0dp,若是垂直布局,控件的宽度不可以设置为0dp。    空间分配:系统会先把 LinearLayout下所有控件指定的 layout_weight值相加,得到一个总值, 然后每个控件所占大小的比例就是用该控件的 layout_weight值除以刚才算出的总值。

Width为数值时,width效果被weight覆盖
详解android四种布局之LinearLayout_第2张图片
Width为数值时,width效果被weight覆盖,xml文件中的代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >       <EditText         android:id="@+id/input_message"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:hint="Type something" />    <Button         android:id="@+id/send"         android:layout_width="0dp"         android:layout_height="wrap_content"        android:layout_weight="1"         android:text="Send" />  LinearLayout>

设置为wrap_content时……weight被覆盖
详解android四种布局之LinearLayout_第3张图片

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <EditText         android:id="@+id/input_message"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_weight="1"         android:hint="Type something" />       <Button         android:id="@+id/send"         android:layout_width="wrap_content"         android:layout_height="wrap_content"        android:layout_weight="1"         android:text="Send" />  LinearLayout>

更多相关文章

  1. android控件背景揭密--NinePatch
  2. Android开发学习:ImageView的scaletype属性
  3. 对android里布局文件当中的TextView对象设置事件监听,但是不响应
  4. Android布局优化之Include(一)
  5. Android spinner控件的实现
  6. android动态创建控件
  7. Android应用开发——TextView控件属性列表

随机推荐

  1. Android 沙箱
  2. Android录音并播放(mp3或amr格式)
  3. OpenGL.Shader:1-重新认识Android上OpenGL
  4. android实现签名功能
  5. Android Compile Error Do not Accept Li
  6. Android Studio实现人民币与美元的转换
  7. Android(安卓)SDK作用和结构
  8. Android7.0中文文档(API)-- AdapterView.On
  9. Android Button的背景图片拉伸变形解决方
  10. 好久米有碎碎念啦 android how to click