LinerLayout 是一行(列)只能放置一個控制項 的線性布局,所以當有很多控制項需要列出來的時候,就可以用這個拉。
代碼:
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textSize="15pt" android:background="#aa0000" android:layout_weight="1" android:text="@string/hang1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textSize="15pt" android:background="#00aa00" android:layout_weight="1" android:text="@string/hang2" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textSize="15pt" android:background="#0000aa" android:layout_weight="1" android:text="@string/hang3" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textSize="15pt" android:background="#aaaa00" android:layout_weight="1" android:text="@string/hang4" /> </LinearLayout>
string.xml中的代碼
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidBookLinerLayoutp101Activity!</string>
<string name="app_name">垂直線性布局</string>
<string name="hang1">第一行</string>
<string name="hang2">第二行</string>
<string name="hang3">第三行</string>
<string name="hang4">第四行</string>
</resources>
解析:
orientation="vertical"表示這個是垂直線性布局
fill_parent 表示該控制項填充整個螢幕
wrap_content表示該控制項隨文字欄位的不同而改變寬度或高度
gravity指明所在位置,上下左右中等等
layout_weight:預設值為0,表示需要多大的視圖就佔據多大的空間; 大於0,則需要按父類別檢視的可用空間進行分割,分割大小取決於該值的大小和在當前所佔的比例
android:text="@string/hang1":引用string裡面的hang1文本。這個很容易理解的了。就這樣規定的
android:background="#aa0000"背景色
把上面的垂直改為 android:orientation="horizontal"水平,就出現這個效果了