編輯:Android 開發實例
LinearLayout 是 ViewGroup 的一種,它會把內部包含的 View 依線性方式排列,方向可以是垂直,也可以是水平。這是最基礎也最常被使用的版面配置之一。
新增一個名為 HelloLinearLayout 的專案,然後打開 res/layout/main.xml 填入以下內容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
最外層是一個 LinearLayout,orientation 設為 vertical,因此它底下兩個子 LinearLayout 會上下堆疊。
第一個子 LinearLayout 採用 horizontal 排列,裡面放了四個彩色底色的 TextView,因此它們會由左到右並排;第二個子 LinearLayout 採用 vertical 排列,裡面的四個 TextView 便會由上到下排成一列。
兩層的 LinearLayout 都設了 layout_weight="1",所以上下兩塊會平分整個螢幕高度。同理,第一層裡的四個 TextView 也都設了 weight,於是四色方塊會等寬並排。
打開 HelloLinearLayout.java,確認它載入了剛才的版面檔:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
setContentView(int) 的作用是載入這個 Activity 的版面檔,而資源 ID R.layout.main 正是指向 res/layout/main.xml。
執行應用後你會看到上半部四個並排的色塊,下半部四行文字。
接著不妨動手改一改:把 android:layout_weight 的值調成不同數字,觀察各個 View 佔用的比例如何變化;也可以試著改 android:orientation,看看排列方向會怎麼轉。版面配置的觀念,靠反覆實驗會比讀文件來得快。
Android 多媒體播放:用 MediaPlayer 實作音樂播放器
Android 提供了多種方式來控制音訊與影片檔案及串流的播放,其中最主要的途徑就是名為 MediaPlayer 的類別。 MediaPlayer 這個類別讓你能存取 A
Android 登入畫面:表單驗證與錯誤次數限制
登入畫面的基本元素登入畫面通常用來收集使用者名稱與密碼。在 Android 中,常使用兩個 EditText 作為輸入欄位,並將密碼欄位設定為密碼輸入型態,避免內容被明顯
動手做「開發者頭條」App(三):首頁整體實現
一、前言 本文是系列第三篇,來把開發者頭條的首頁搭起來。先看效果: 從動圖可以拆出兩層結構:外層是「精選、訂閱、發現」三個可左右滑動的 Tab;進入精選頁後,頂部是一塊
動手做安卓文件管理器(一):先理清資源層
文件管理器這類工具的職責很明確:幫用戶把存在本地和網絡上的文件管好,創建、打開、查看、編輯、移動、刪除是最基本的六件事,做得深一點還會加上網絡掛載、應用管理、解壓縮和全文