網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、線性布局LinearLayout
有兩種排序方式
- orientation屬性值為horizontal時(shí),內(nèi)部視圖在水平方向從左往右排列。
- orientation屬性值為vertical時(shí),內(nèi)部視圖在垂直方向從上往下排列。
如果不指定orientation屬性,則LinearLayout默認(rèn)水平方向排列。
線性布局的權(quán)重
指線性布局的下級(jí)視圖各自擁有多大比例的寬高。
屬性名為layout_weight,但該屬性不在LinearLayout節(jié)點(diǎn)設(shè)置,而在線性布局的直接下級(jí)視圖設(shè)置,表示改下級(jí)視圖占據(jù)的寬高比例。
- layout_width為0dp時(shí),表示水平方向的寬度比例
- layout_height為0dp時(shí),表示垂直方向的高度比例
例:
第一個(gè)線性布局:width = 0dp 說(shuō)明在水平方向設(shè)置寬度比例,weight = 1,占據(jù)weight總數(shù)的1/2,則占據(jù)一半空間。
第二個(gè)線性布局:height = 0dp 說(shuō)明在垂直方向設(shè)置寬度比例,weight = 1,占據(jù)weight總數(shù)的1/3,則占據(jù)三分之一空間。
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp"http://寬度為0dp,通過(guò)權(quán)重設(shè)置寬度比例 android:layout_height="wrap_content" android:layout_weight="1"http://weight為1,下面的weight也為1,占1/2,即寬度比例占1/2 android:text="橫排第一個(gè)" android:textSize="17sp" android:textColor="#000000"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="橫排第二個(gè)" android:textSize="17sp" android:textColor="#000000"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="0dp"http://高度為0dp,通過(guò)權(quán)重設(shè)置高度比例 android:layout_weight="1"http://weight為1,下面的weight為2,占1/3,即寬度比例占1/3 android:text="豎排第一個(gè)" android:textSize="17sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="2" android:text="豎排第二個(gè)" android:textSize="17sp" android:textColor="#000000"/> </LinearLayout>
二、相對(duì)布局RelativeLayout
相對(duì)布局的視圖位置由平級(jí)或上級(jí)視圖決定,用于確定下級(jí)視圖位置的參考物分兩種:
- 與該視圖自身平級(jí)的視圖
- 該視圖的上級(jí)視圖
如果不設(shè)定下級(jí)視圖的參照物,那么下級(jí)視圖默認(rèn)顯示在RelativeLayout內(nèi)部的左上角。
相對(duì)位置的取值
例:
<TextView android:id="@+id/tv_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_centerInParent="true" android:text="中間" android:textSize="11sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_centerHorizontal="true" android:text="水平中間" android:textSize="11sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_centerVertical="true" android:text="垂直中間" android:textSize="11sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_alignParentLeft="true" android:text="上級(jí)左邊對(duì)齊" android:textSize="11sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_toLeftOf="@id/tv_center" android:layout_alignTop="@id/tv_center" android:text="中間左邊" android:textSize="11sp" android:textColor="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:layout_above="@id/tv_center" android:layout_alignLeft="@id/tv_center" android:text="中間上邊" android:textSize="11sp" android:textColor="#000000"/>
三、網(wǎng)格布局GridLayout
網(wǎng)格布局支持多行多列的表格排列。
網(wǎng)格布局默認(rèn)從左往右、從上到下排列,新增兩個(gè)屬性:
- columnCount屬性:指定網(wǎng)格的列數(shù),即每行能放多少視圖。
- rowCount屬性:指定網(wǎng)格行數(shù),即每列能放多少視圖。
例:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:rowCount="2"> <TextView android:layout_width="0dp"http://設(shè)置權(quán)重,占滿屏幕 android:layout_columnWeight="1" android:layout_height="60dp" android:background="#ffcccc" android:text="淺紅色" android:gravity="center"http://設(shè)置文字位于網(wǎng)格中間 android:textColor="#000000" android:textSize="17sp"/> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#ffaa00" android:text="淺紅色" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#00ff00" android:text="綠色" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#660066" android:text="深紫色" android:gravity="center" android:textColor="#000000" android:textSize="17sp"/> </GridLayout>
四、滾動(dòng)視圖ScrollView
有兩種:
- ScrollView:垂直方向的滾動(dòng)視圖,垂直方向滾動(dòng)時(shí),layout_width屬性值設(shè)置為match_parent,layout_height 屬性值設(shè)置為wrap_content。
- HorizontalScrollView:水平方向的滾動(dòng)視圖,水平方向滾動(dòng)時(shí),layout_width屬性值設(shè)置為wrap_content,layout_height屬性值設(shè)置為match_parent。
例:
水平方向兩個(gè)View共600dp,超出屏幕,所以上級(jí)視圖使用HorizontalScrollView,寬度自適應(yīng),高度跟隨上級(jí)視圖。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="200dp"> <!-- 水平方向的線性布局--> <LinearLayout android:layout_width="wrap_content"http://寬度自適應(yīng) android:layout_height="match_parent"http://高度跟隨上級(jí)視圖 android:orientation="horizontal">//水平排列 <View android:layout_width="300dp"http://寬度自定義,超出屏幕 android:layout_height="match_parent" android:background="#aaffff"/> <View android:layout_width="300dp" android:layout_height="match_parent" android:background="#ffff00"/> </LinearLayout> </HorizontalScrollView> <!-- 垂直方向的線性布局--> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="400dp" android:background="#aaffff"/> <View android:layout_width="match_parent" android:layout_height="400dp" android:background="#ffff00"/> </LinearLayout> </ScrollView> </LinearLayout>
原文鏈接:https://blog.csdn.net/Tir_zhang/article/details/126783783
相關(guān)推薦
- 2023-10-26 解決:NODE_ENV 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序,或者批處理文件
- 2022-04-08 python?selenium保存圖片最好的兩種方法_python
- 2022-11-11 python貪吃蛇核心功能實(shí)現(xiàn)下_python
- 2022-05-20 ASP.NET?MVC項(xiàng)目部署方式介紹_基礎(chǔ)應(yīng)用
- 2022-06-19 C++詳細(xì)分析講解函數(shù)參數(shù)的擴(kuò)展_C 語(yǔ)言
- 2022-07-21 提高新手寫代碼效率的Emmet插件怎么使用
- 2023-05-21 詳解Python中文件路徑_python
- 2022-06-23 Android?Socket實(shí)現(xiàn)多個(gè)客戶端聊天布局_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支