編輯:關於 Android 開發
LinearLayout 是 ViewGroup 的一種,其中的子 View 會依照線性方式排列,方向可以是垂直(vertical)或水平(horizontal)。本文透過一個 HelloLinearLayout 範例,示範如何透過巢狀的 LinearLayout 組合出多層次的畫面。
新增一個名為 HelloLinearLayout 的專案,並開啟 res/layout/main.xml 進行修改。
最外層是一個 orientation 設為 vertical 的 LinearLayout,內含兩個子 LinearLayout:第一個採水平排列,第二個採垂直排列,每個子 LinearLayout 又各自包含數個 TextView。
<?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>
開啟 HelloLinearLayout.java,確認它載入了 main.xml:
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(搭配 layout_width/layout_height)的值,觀察各個 View 的顯示比例如何隨之改變,這也是理解權重配置最快速的方式。
Android Studio 自學筆記(目錄):從環境建置到界面開發
一套完整的 Android 自學路線 這份筆記整理了我使用 Android Studio 學習 Android 開發的完整歷程,適合想系統性入門的朋友作為參考路線圖。 開
Android 自定義控件(二):優酷菜單三層界面的布局搭建
系列目標 本系列將逐步還原優酷客戶端那個經典的三層扇形菜單效果: 原文地址:https://www.cnblogs.com/wuyudong/p/5912538.htm
HelloCharts 線形圖入門
HelloCharts 線形圖入門 HelloCharts 是一個輕量化的 Android 圖表框架,支援線形圖、柱狀圖、圓餅圖等多種圖表。本文以線形圖為例,說明如何建立
MPAndroidChart 圖表庫實戰:餅圖、折線圖與柱狀圖一次學會
用 MPAndroidChart 打造靈活的統計圖表 MPAndroidChart 是一款相當好用的開源圖表庫,它不僅能在 Android 裝置上繪製各種類型的統計圖表,