Android Layout之三:Linear Layout

转载自:http://android.blog.51cto.com/268543/298345

线形布局

orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列,horizontal: 子元素们水平排列。在代码里可通过setOrientation()进行动态改变,值分别为HORIZONTAL或者VERTICAL。 * 在Linear Layout, 宽度/高度都是按着组件的次序逐个占用的!所以当某个组件设置"fill_parent",在没有设置Layout_weight的情况下,该组件会占用了余下的空间,那么在它后面的组件就会显示不出来。如下图的EditText如果没有设置android:layout_weight="1", 它下面的其他组件就看不见了! baselineAligned 一般情况下,这个属性默认为true,代表在同一方向的组件都基于第一个组件对齐。所以可以看到下图的text1, button1, text2是在同一水平线的。当不需要这效果时,可以设置为false。 main.xml配置内容:
<?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:text="This is a example of linear layout."         android:layout_width="fill_parent"          android:layout_height="wrap_content"          />     <EditText         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1"         android:id="@+id/edittext"         />      <LinearLayout         android:id="@+id/LinearLayout01"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:orientation="horizontal">         <TextView              android:text="text1"              android:id="@+id/TextView01"              android:layout_width="wrap_content"              android:layout_height="wrap_content"             />         <Button android:text="Button01"              android:id="@+id/Button01"              android:layout_width="fill_parent"              android:layout_height="wrap_content"             android:layout_weight="1"             />         <TextView              android:text="text2"              android:id="@+id/TextView02"              android:layout_width="wrap_content"              android:layout_height="wrap_content"             />     </LinearLayout>      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="buttom"         /> </LinearLayout>
结果如下图:

如果将main.xml配置内容中:

<EditText         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="1"         android:id="@+id/edittext"         /> 

更改为:

<EditText         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:layout_weight="0"         android:id="@+id/edittext"         /> 

或者:

<EditText         android:layout_width="fill_parent"         android:layout_height="fill_parent"                 android:id="@+id/edittext"         /> 

那么,结果如下图:



发现组件edittext将下面的组件全部挡住了,这其中就数属性 【layout_weight 】导致的。

layout_weight - 重要度

个人理解为显示的优先级。默认为0(最高),数值越大,优先级越低!参考下面的Linear Layout例子。要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height = "fill_parent!

更多相关文章

  1. Android之基本组件
  2. Android------Button 添加声音效果(两种方式)
  3. Android(安卓)XML属性在文档中的位置
  4. android (一)RecycleView组件的使用
  5. 你了解Android中的Activity吗?
  6. Android多个Activity
  7. BroadcastReceiver(三)广播消息的优先级与中断
  8. android 组件使用()
  9. 详解Android中通过Intent类实现组件间调用的方法

随机推荐

  1. Android Socket编程
  2. android 动态渐变 字符效果
  3. Android Studio打包时出现transformClass
  4. Android 获取 IP 地址
  5. Android 各种Dialog例子
  6. android地图获取当前位置,气泡标志,点击气
  7. Android animation - 文字旋转示例
  8. ScrollNumber 数字滚动
  9. Android热补丁动态修复技术(四):完善框架①
  10. Android 为CheckBoxPreference Preferenc