Android UI控制元件系列:LinearLayout(線性佈局)
LinearLayout是線上性方向顯示View元素的一個ViewGroup,可以是水平方向,也可以是垂直方向
你可以重複使用LinearLayout,如果你想使用巢狀多層的LinearLayout的話,你可以考慮使用RelativeLayout來替換.
1、開始建立一個工程名字叫做HelloLinearLayout
2、開啟res/layout/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" > <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>
仔細檢查這個XML檔案。有一個根元素LinearLayout定義了它的方向是垂直的,所有的子View(一共有2個)都是被垂直方向堆起的,第一個子孩子是另一個以水平方向佈局的LinearLayout,並且第二個子孩子是一個用垂直方向佈局的LinearLayout,這些每一個被巢狀的LinearLayout都包含幾個TextView元素,它們的方向是由父LinearLayout標籤所定義。
3、現在開啟HelloLinearLayout.java並且確定它已經在onCreate()方法中載入了res/layout/main.xml佈局檔案
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
setContentView(int)方法為Activity載入了佈局檔案,由資源resource ID所指定—R.layout.main指的是res/layout/main.xml佈局檔案
4、執行程式,你可以看到如下的情況
相關文章
- Android入門教程 | UI佈局之LinearLayout 線性佈局AndroidUI
- Android入門教程 | UI佈局之RelativeLayout 相對佈局AndroidUI
- DirectionalLayout線性佈局
- Flutter學習指南:UI佈局和控制元件FlutterUI控制元件
- 阿里Android開發規範:UI 與佈局阿里AndroidUI
- SAP UI5 Form 控制元件的 ColumnLayout 佈局特性UIORM控制元件
- Flutter線性佈局Row和ColumnFlutter
- Android學習筆記之檔案分類和線性佈局Android筆記
- Android 佈局Android
- ScrollView巢狀LinearLayout佈局不能撐滿全屏的問題View巢狀
- 如何控制 LinearLayout 優先顯示右邊的佈局,空間不足時擠壓左邊控制元件?控制元件
- Android Material Design控制元件使用(一)——ConstraintLayout 約束佈局AndroidMaterial Design控制元件AI
- Android開發 - 檢視佈局屬性解析Android
- 彈性佈局(伸縮佈局)
- css佈局系列1——盒模型佈局CSS模型
- Android學習—— Android佈局Android
- 八、android當中五大布局控制元件。其它常用佈局Android控制元件
- QT佈局之控制元件空隙QT控制元件
- tkinter中frame佈局控制元件控制元件
- 組合控制元件的佈局控制元件
- flex彈性佈局 響應式佈局Flex
- Android 佈局優化Android優化
- android --巧用 flexboxLayout 佈局AndroidFlex
- 【5年Android從零覆盤系列之五】關於頁面佈局控制元件開發總結Android控制元件
- Android中常見的佈局和佈局引數Android
- [譯]在 Flutter 中如何設計線性佈局?Flutter
- HarmonyOS Java UI之DirectionalLayout佈局JavaUI
- css經典佈局系列一——垂直居中佈局CSS
- SAP UI5 應用 SimpleForm 控制元件 ResponsiveGridLayout 佈局的工作原理深入剖析UIORM控制元件
- flex佈局屬性Flex
- Flutter 佈局控制元件完結篇Flutter控制元件
- WPF自定義FixedColumnGrid佈局控制元件控制元件
- Flutter 佈局控制元件篇-->Row、ColumnFlutter控制元件
- Flutter 佈局控制元件篇-->Flex、ExpandedFlutter控制元件Flex
- Flutter 佈局控制元件篇-->Stack、PositionedFlutter控制元件
- Flutter 佈局控制元件篇-->Wrap、FlowFlutter控制元件
- 【Android】設定 LinearLayout 的樣式Android
- css經典佈局系列二——等分等高佈局CSS
- Android FlexboxLayout 佈局詳解AndroidFlex