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!