網站首頁 編程語言 正文
安卓的開發從布局開始。
安卓的界面編寫也是使用xml
進行布局的,一般如果熟悉了html
界面的布局,那么很容易就能夠理解安卓有關的布局了,這里介紹兩個比較重要的布局方式:線性布局(LinearLayout)和相對布局(RelativeLayout)。
新建的功能布局,一般是一個界面對應一個xml
文件,main
界面的xml
在activity_main.xml
中。
線性布局LinearLayout
根據名字我們就很清楚,線性布局的意思了,相當于html中的div層,兩種布局方向:
vertical
下的布局方式:
horizontal
下的布局方式:
vertical
布局代碼:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#00aaff" > <Button android:id = "@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Button" /> <Button android:id = "@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Button" /> </LinearLayout>
horizontal
下的布局代碼:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#A6A7AF" > <Button android:id = "@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="Button" /> <Button android:id = "@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="Button" /> </LinearLayout>
有幾個屬性需要熟悉一下:
- wrap_content 為按照控件內容的大小進行調整
- layout_marginLeft 為控件左邊的偏移,其他的一次類推
- layout_gravity 可以用來進行控件居中顯示
- layout_weight 控件在horizontal模式下占到的比率
相對布局RelativeLayout
相對布局 主要兩種相當模式,一種是父控件,一種是相對兄弟控件。
布局代碼如下:
<RelativeLayout android:layout_width="match_parent" android:layout_height="300dp" android:background="#9C27B0" > <Button android:id = "@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:text="Button5" /> <Button android:id = "@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Button6" /> <Button android:id = "@+id/btn7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_below="@+id/btn6" android:text="Button7" /> </RelativeLayout>
幾個重要的布局:
- layout_alignParentxxxx 相對于父類的情況
- layout_to 相對于兄弟的情況
項目在github的地址在這里。
小結
布局的方式比較多,但是這兩個種布局方式是最重要的,也可以這么說掌握了這兩種以后,其他的就可以依次類推,只要知道里面的屬性基本上就容易上手了。
原文鏈接:https://blog.csdn.net/weixin_40425640/article/details/127850722
相關推薦
- 2022-08-04 Python練習之讀取XML節點和屬性值的方法_python
- 2022-12-08 C語言實現計算圓周長以及面積_C 語言
- 2023-03-16 Android虛擬機Dalvik和ART科普_Android
- 2023-01-28 Flutter交互并使用小工具管理其狀態widget的state詳解_Android
- 2022-07-31 C++超詳細分析講解內聯函數_C 語言
- 2022-10-09 .NET擴展方法使用實例詳解_實用技巧
- 2022-08-26 Rust+React創建富文本編輯器_React
- 2022-12-07 進程狀態ps?-ef中的e、f含義講解_linux shell
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支