一起學Android之Layout

来源:https://www.cnblogs.com/hsiang/archive/2018/12/14/10122215.html
-Advertisement-
Play Games

本文簡述在Android開發中佈局的簡單應用,屬於基礎知識,僅供學習分享使用。 ...


本文簡述在Android開發中佈局的簡單應用,屬於基礎知識,僅供學習分享使用。

概述

在Android UI開發中,佈局類型主要有兩種:LinearLayout(線性佈局)和RelativeLayout(相對佈局),兩種佈局類型各有各的優勢與使用場景。

LinearLayout(線性佈局)

線性佈局允許所有的子元素,以單獨的方向進行排列(水平或垂直),所有的元素像棧一樣一個接一個的插入,所以如果是垂直(vertical)方向,則每一行只有一個元素。如果是水平( horizontal)方向,則只有一行。(如下圖1所示)

線性佈局重要屬性

android:orientation 設置排列的方向。主要有兩個值:horizontal(水平),vertical(垂直)。

android:layout_weight 權重,按比例分配剩餘空間。

 

        (圖1)                                                       (圖2)

RelativeLayout(相對佈局)

相對佈局是指所有子元素以相對的位置進行定位。一個元素可以通過相對於指定的同級元素(如,左邊,右邊,上邊,下邊)進行定位,也可以通過父元素進行定位(如,佈局控制項的頂端,左端,右端,底部等)(如上圖2 所示)。如果發現頁面中有多個線性佈局進行嵌套,那麼你就應該用一個相對佈局來替換它。

相對佈局重要屬性

  • android:layout_alignParentTop 是否位於父控制項的部(true 或 false)
  • android:layout_alignParentBottom 是否位於父控制項的部(true 或 false)
  • android:layout_alignParentLeft 是否位於父控制項的邊(true 或 false)
  • android:layout_alignParentRight 是否位於父控制項的邊(true 或 false)
  • android:layout_centerInParent 是否位於父控制項的中心(true 或 false)
  • android:layout_toLeftOf 位於指定控制項的邊(值為控制項的ID)
  • android:layout_toRightOf 位於指定控制項的邊(值為控制項的ID)
  • android:layout_above 位於指定控制項的邊(值為控制項的ID)
  • android:layout_below 位於指定控制項的邊(值為控制項的ID)

實例截圖

如下圖1所示為線性佈局(相對簡單),如下圖2所示,為相對佈局(相對複雜)

 

                     圖3                                                                         圖4

佈局源程式

線性佈局

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout
  3     xmlns:android="http://schemas.android.com/apk/res/android"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     android:paddingBottom="@dimen/activity_vertical_margin"
  8     android:paddingLeft="@dimen/activity_horizontal_margin"
  9     android:paddingRight="@dimen/activity_horizontal_margin"
 10     android:paddingTop="@dimen/activity_vertical_margin"
 11     android:orientation="vertical"
 12     tools:context="com.hex.demolayout.LinearActivity">
 13     <TextView
 14         android:id="@+id/tv_text"
 15         android:text="@string/tv_name"
 16         android:textSize="20dp"
 17         android:layout_marginTop="5dp"
 18         android:textColor="@color/colorBlue"
 19         android:layout_width="match_parent"
 20         android:layout_height="wrap_content"/>
 21     <EditText
 22         android:id="@+id/txt_name"
 23         android:hint="@string/hint_name"
 24         android:layout_width="match_parent"
 25         android:layout_height="wrap_content"/>
 26     <TextView
 27         android:id="@+id/tv_age"
 28         android:text="@string/tv_age"
 29         android:textSize="20dp"
 30         android:layout_marginTop="5dp"
 31         android:textColor="@color/colorBlue"
 32         android:layout_width="match_parent"
 33         android:layout_height="wrap_content"/>
 34     <EditText
 35         android:id="@+id/txt_age"
 36         android:hint="@string/hint_name"
 37         android:layout_width="match_parent"
 38         android:layout_height="wrap_content"/>
 39     <TextView
 40         android:id="@+id/tv_sex"
 41         android:text="@string/tv_sex"
 42         android:textSize="20dp"
 43         android:layout_marginTop="5dp"
 44         android:textColor="@color/colorBlue"
 45         android:layout_width="match_parent"
 46         android:layout_height="wrap_content"/>
 47     <RadioGroup
 48         android:id="@+id/rg_sex"
 49         android:layout_marginTop="5dp"
 50         android:orientation="horizontal"
 51         android:layout_width="match_parent"
 52         android:layout_height="wrap_content">
 53         <RadioButton
 54             android:id="@+id/rb_male"
 55             android:text="@string/male"
 56             android:textSize="20sp"
 57             android:checked="true"
 58             android:layout_width="wrap_content"
 59             android:layout_height="wrap_content"/>
 60         <RadioButton
 61             android:id="@+id/rb_female"
 62             android:textSize="20sp"
 63             android:text="@string/female"
 64             android:layout_marginLeft="30dp"
 65             android:layout_width="wrap_content"
 66             android:layout_height="wrap_content"/>
 67     </RadioGroup>
 68     <TextView
 69         android:id="@+id/tv_love"
 70         android:text="@string/love"
 71         android:textSize="20dp"
 72         android:layout_marginTop="5dp"
 73         android:textColor="@color/colorBlue"
 74         android:layout_width="match_parent"
 75         android:layout_height="wrap_content"/>
 76     <LinearLayout
 77         android:layout_width="match_parent"
 78         android:layout_height="wrap_content"
 79         android:layout_marginTop="10dp"
 80         android:orientation="horizontal">
 81         <CheckBox
 82             android:id="@+id/ck_basketball"
 83             android:text="@string/basketball"
 84             android:textSize="20dp"
 85             android:layout_width="wrap_content"
 86             android:layout_height="wrap_content"/>
 87         <CheckBox
 88             android:id="@+id/ck_football"
 89             android:text="@string/football"
 90             android:textSize="20dp"
 91             android:layout_marginLeft="30dp"
 92             android:layout_width="wrap_content"
 93             android:layout_height="wrap_content"/>
 94         <CheckBox
 95             android:id="@+id/ck_game"
 96             android:text="@string/game"
 97             android:textSize="20dp"
 98             android:layout_marginLeft="30dp"
 99             android:layout_width="wrap_content"
100             android:layout_height="wrap_content"/>
101     </LinearLayout>
102     <TextView
103         android:id="@+id/tv_school"
104         android:text="@string/school"
105         android:textSize="20dp"
106         android:layout_marginTop="5dp"
107         android:textColor="@color/colorBlue"
108         android:layout_width="match_parent"
109         android:layout_height="wrap_content"/>
110     <EditText
111         android:id="@+id/txt_school"
112         android:hint="@string/hint_school"
113         android:layout_width="match_parent"
114         android:layout_height="wrap_content"/>
115     <TextView
116         android:id="@+id/tv_addr"
117         android:text="@string/addr"
118         android:textSize="20dp"
119         android:layout_marginTop="5dp"
120         android:textColor="@color/colorBlue"
121         android:layout_width="match_parent"
122         android:layout_height="wrap_content"/>
123     <EditText
124         android:id="@+id/txt_addr"
125         android:hint="@string/hint_addr"
126         android:layout_width="match_parent"
127         android:layout_height="wrap_content"/>
128     <Button
129         android:id="@+id/bn_submit"
130         android:text="@string/bn_submit"
131         android:textColor="@color/colorBlue"
132         android:layout_width="match_parent"
133         android:layout_height="wrap_content"/>
134 </LinearLayout>
View Code

相對佈局

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout
  3     xmlns:android="http://schemas.android.com/apk/res/android"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     android:paddingBottom="@dimen/activity_vertical_margin"
  8     android:paddingLeft="@dimen/activity_horizontal_margin"
  9     android:paddingRight="@dimen/activity_horizontal_margin"
 10     android:paddingTop="@dimen/activity_vertical_margin"
 11     tools:context="com.hex.demolayout.MainActivity">
 12 
 13     <TextView
 14         android:id="@+id/tv_title"
 15         android:layout_centerHorizontal="true"
 16         android:layout_alignParentTop="true"
 17         android:layout_width="wrap_content"
 18         android:layout_height="wrap_content"
 19         android:textSize="30dp"
 20         android:textColor="@color/colorBlue"
 21         android:layout_margin="3dp"
 22         android:text="@string/nine_tip"/>
 23     <TextView
 24         android:id="@+id/tv_center"
 25         android:text="@string/center"
 26         android:textSize="30dp"
 27         android:layout_margin="3dp"
 28         android:onClick="open"
 29         android:textColor="@color/colorRed"
 30         android:layout_centerInParent="true"
 31         android:layout_width="wrap_content"
 32         android:layout_height="wrap_content"/>
 33     <TextView
 34         android:id="@+id/tv_east"
 35         android:text="@string/east"
 36         android:textSize="30dp"
 37         android:layout_margin="3dp"
 38         android:textColor="@color/colorRed"
 39         android:layout_alignParentLeft="true"
 40         android:layout_centerVertical="true"
 41         android:layout_width="wrap_content"
 42         android:layout_height="wrap_content"/>
 43     <TextView
 44         android:id="@+id/tv_west"
 45         android:text="@string/west"
 46         android:textSize="30dp"
 47         android:layout_margin="3dp"
 48         android:textColor="@color/colorRed"
 49         android:layout_alignParentRight="true"
 50         android:layout_centerVertical="true"
 51         android:layout_width="wrap_content"
 52         android:layout_height="wrap_content"/>
 53     <TextView
 54         android:id="@+id/tv_north"
 55         android:text="@string/north"
 56         android:textSize="30dp"
 57         android:layout_margin="3dp"
 58         android:textColor="@color/colorRed"
 59         android:layout_alignParentBottom="true"
 60         android:layout_centerHorizontal="true"
 61         android:layout_width="wrap_content"
 62         android:layout_height="wrap_content"/>
 63     <TextView
 64         android:id="@+id/tv_south"
 65         android:text="@string/south"
 66         android:textSize="30dp"
 67         android:layout_margin="3dp"
 68         android:textColor="@color/colorRed"
 69         android:layout_below="@id/tv_title"
 70         android:layout_centerHorizontal="true"
 71         android:layout_width="wrap_content"
 72         android:layout_height="wrap_content"/>
 73     <TextView
 74         android:id="@+id/tv_east_south"
 75         android:text="@string/east_south"
 76         android:textSize="30dp"
 77         android:layout_margin="3dp"
 78         android:textColor="@color/colorRed"
 79         android:layout_below="@id/tv_title"
 80         android:layout_alignParentLeft="true"
 81         android:layout_width="wrap_content"
 82         android:layout_height="wrap_content"/>
 83     <TextView
 84         android:id="@+id/tv_west_south"
 85         android:text="@string/west_south"
 86         android:textSize="30dp"
 87         android:layout_margin="3dp"
 88         android:textColor="@color/colorRed"
 89         android:layout_below="@id/tv_title"
 90         android:layout_alignParentRight="true"
 91         android:layout_width="wrap_content"
 92         android:layout_height="wrap_content"/>
 93     <TextView
 94         android:id="@+id/tv_east_north"
 95         android:text="@string/east_north"
 96         android:textSize="30dp"
 97         android:layout_margin="3dp"
 98         android:textColor="@color/colorRed"
 99         android:layout_alignParentBottom="true"
100         android:layout_width="wrap_content"
101         android:layout_height="wrap_content"/>
102     <TextView
103         android:id="@+id/tv_west_north"
104         android:text="@string/west_north"
105         android:textSize="30dp"
106         android:layout_margin="3dp"
107         android:textColor="@color/colorRed"
108         android:layout_alignParentBottom="true"
109         android:layout_alignParentRight="true"
110         android:layout_width="wrap_content"
111         android:layout_height="wrap_content"/>
112     <TextView
113         android:id="@+id/tv_xun"
114         android:text="@string/xun"
115         android:textSize="30dp"
116         android:layout_margin="3dp"
117         android:textColor="@color/colorGreen"
118         android:layout_below="@id/tv_east_south"
119         android:layout_width="wrap_content"
120         android:layout_height="wrap_content"/>
121     <TextView
122         android:id="@+id/tv_li"
123         android:text="@string/li"
124         android:textSize="30dp"
125         android:layout_margin="3dp"
126         android:textColor="@color/colorGreen"
127         android:layout_below="@id/tv_south"
128         android:layout_centerHorizontal="true"
129         android:layout_width="wrap_content"
130         android:layout_height="wrap_content"/>
131     <TextView
132         android:id="@+id/tv_kun"
133         android:text="@string/kun"
134         android:textSize="30dp"
135         android:layout_margin="3dp"
136         android:textColor="@color/colorGreen"
137         android:layout_below="@id/tv_west_south"
138         android:layout_alignParentRight="true"
139         android:layout_width="wrap_content"
140         android:layout_height="wrap_content"/>
141     <TextView
142         android:id="@+id/tv_zen"
143         android:text="@string/zen"
144         android:textSize="30dp"
145         android:layout_margin="3dp"
146         android:textColor="@color/colorGreen"
147         android:layout_toRightOf="@id/tv_east"
148         android:layout_centerVertical="true"
149         android:layout_width="wrap_content"
150         android:layout_height="wrap_content"/>
151     <TextView
152         android:id="@+id/tv_dui"
153         android:text="@string/dui"
154         android:textSize="30dp"
155         android:layout_margin="3dp"
156         android:textColor="@color/colorGreen"
157         android:layout_toLeftOf="@id/tv_west"
158         android:layout_centerVertical="true"
159         android:layout_width="wrap_content"
160         android:layout_height="wrap_content"/>
161     <TextView
162         android:id="@+id/tv_gen"
163         android:text="@string/gen"
164         android:textSize="30dp"
165         android:layout_margin="3dp"
166         android:textColor="@color/colorGreen"
167         android:layout_above="@id/tv_east_north"
168         android:layout_width="wrap_content"
169         android:layout_height="wrap_content"/>
170     <TextView
171         android:id="@+id/tv_kan"
172         android:text="@string/kan"
173         android:textSize="30dp"
174         android:layout_margin="3dp"
175         android:layout_above="@id/tv_north"
176         android:textColor="@color/colorGreen"
177         android:layout_centerHorizontal="true"
178         android:layout_width="wrap_content"
179         android:layout_height="wrap_content"/>
180     <TextView
181         android:id="@+id/tv_qan"
182         android:text="@string/qan"
183         android:textSize="30dp"
184         android:layout_margin="3dp"
185         android:textColor="@color/colorGreen"
186         android:layout_above="@id/tv_west_north"
187         android:layout_alignRight="@id/tv_west_north"
188         android:layout_width="wrap_content"
189         android:layout_height="wrap_content"/>
190     <TextView
191         android:id="@+id/tv_mu"
192         android:text="@string/mu"
193         android:textSize="30dp"
194         android:layout_margin="3dp"
195         android:textColor="@color/colorBlue"
196         android:layout_below="@id/tv_xun"
197         android:layout_width="wrap_content"
198         android:layout_height="wrap_content"/>
199     <TextView
200         android:id="@+id/tv_huo"
201         android:text="@string/huo"
202         android:textSize="30dp"
203         android:layout_margin="3dp"
204         android:textColor="@color/colorBlue"
205         android:layout_below="@id/tv_li"
206         android:layout_centerHorizontal="true"
207         android:layout_width="wrap_content"
208         android:layout_height="wrap_content"/>
209     <TextView
210         android:id="@+id/tv_tu"
211         android:text="@string/tu"
212         android:textSize="30dp"
213         android:layout_margin="3dp"
214         android:textColor="@color/colorBlue"
215         android:layout_below="@id/tv_kun"
216         android:layout_alignParentRight="true"
217         android:layout_width="wrap_content"
218         android:layout_height="wrap_content"/>
219     <TextView
220         android:id="@+id/tv_mu2"
221         android:text="@string/mu"
222         android:textSize="30dp"
223         android:layout_margin="3dp"
224         android:textColor="@color/colorBlue"
225         android:layout_below="@id/tv_zen"
226         android:layout_alignLeft="@id/tv_zen"
227         android:layout_width="wrap_content"
228         android:layout_height="wrap_content"/>
229     <TextView
230         android:id="@+id/tv_tu2"
231         android:text="@string/tu"
232         android:textSize="30dp"
233         android:layout_margin="3dp"
234         android:textColor="@color/colorBlue"
235         android:layout_below="@id/tv_center"
236         android:layout_alignLeft="@id/tv_center"
237         android:layout_width="wrap_content"
238         android:layout_height="wrap_content"/>
239     <TextView
240         android:id="@+id/tv_jin"
241         android:text="@string/jin"
242         android:textSize="30dp"
243         android:textColor="@color/colorBlue"
244         android:layout_margin="3dp"
245         android:layout_below="@id/tv_dui"
246         android:layout_alignLeft="@id/tv_dui"
247         android:layout_width="wrap_content"
248         android:layout_height="wrap_content"/>
249     <TextView
250         android:id="@+id/tv_tu3"
251         android:text="@string/tu"
252         android:textSize="30dp"
253 	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 獲取今天的00:00:00 SELECT CONVERT(DATETIME,CONVERT(VARCHAR(10),GETDATE(),120)) 獲取今天的23:59:59 1、SELECT DATEADD(SS,-1,DATEADD(DD,1,CONVERT(DATETIME,CONVERT(V ...
  • 本來按理說這個小問題不值得寫一個博客的,不過正是這個小問題造成了一個大bug。 本來每月對數據都好好的,但是這一兩天突然發現許多數據明顯不對,這一塊的代碼和sql有些不是我寫的,不過出現了bug,還是要迎難而上,我就從數據源頭查起,發現數據源好像也沒有問題。 地毯式搜索,中間的sql,邏輯一個一個對 ...
  • 一.概述 Sentinel(哨崗或哨兵)是Redis的高可用解決方案:由一個或多個Sentinel實例(instance)組成的Sentinel系統(system)可以監視任意多個主伺服器,以及這些主伺服器屬下的所有從伺服器,併在被監視的主伺服器進入下線狀態時,自動將下線主伺服器屬下的某個從伺服器升 ...
  • ...
  • 下載需要的版本的xtrabackup軟體包,鏈接如下: https://www.percona.com/downloads/XtraBackup/LATEST/ percona-xtrabackup-8.0.4只支持mysql8.0及以上版本備份,如果想備份8.0以下版本MySQL,需要下載perc ...
  • 恢復內容開始 ODI流程 Topology 1、建立 源 物理結構體系 2、建立 目的 物理結構體系 步驟同上 3、建立 源 邏輯架構 4、建立 目的 邏輯架構 步驟同上 Designer 5、建立 源 模型 點擊 6、建立 目的 模型 步驟同上 7、建立項目 8、 導入模塊 路徑為 :F:\Ora ...
  • 1 /*修改欄位類型*/ 2 alter table 表名 ALTER COLUMN 列名 nvarchar(500) 3 go 4 /*增加欄位和說明*/ 5 alter table 表名 add 列名 nvarchar(50) 6 EXECUTE sp_addextendedproperty N... ...
  • map reduce的解釋 這是一張來自mongodb mapreduce圖示,比較能說明問題 其實我們可以從word count這個實例來理解MapReduce。MapReduce大體上分為六個步驟:input, split, map, shuffle, reduce, output。細節描述如下 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...