Android_Layout (一)

layout (布局) ?--->Android 有五大布局,分別是

  • LinearLayout :?線性布局,子組件按照垂直或者水平方向來布局。
  • RelativeLayout :相對布局,按照控件之間的相互位置進行參照物的概念排布,存在一個排布,存在一個參照物的概念, 一般來說在相對布局中的控件都會存在id 的屬性。
  • TableLayout :表格布局,繼承于線性布局。(用法類似)
  • AbsoluteLayout?絕對布局,是通過指定控件的x/y坐標來定位的(顯示效果死板,不利于開發,幾乎被棄用)。
  • FrameLayout?幀布局,畫面顯示的效果

一、線性布局(LiearLayout) 

?  ?線性布局在開發中使用最多,具有垂直方向與水平方向的布局方式
  通過設置屬性“android:orientation”控制方向,屬性值垂直(vertical)和水平(horizontal),默認水平方向。

 常用屬性:

  android:layout_gravity 本元素相對于父元素的重力方向

  android:gravity 本元素所有子元素的重力方向

  android:orientation 線性布局以列或行來顯示內部子元素

  android:layout_weight 子元素對未占用空間水平或垂直分配權重值


  當 android:orientation="vertical" 時, 只有水平方向的設置才起作用,垂直方向的設置不起作用。即:left,right,center_horizontal 是生效的。!!!!

  當 android:orientation="horizontal" 時, 只有垂直方向的設置才起作用,水平方向的設置不起作用。即:top,bottom,center_vertical 是生效的。!!!!

android:layout_gravity 和 android:gravity 的區別

  android:gravity?對元素本身起作用-本身元素顯示在什么位置

  android:layout_gravity?相對與它的父元素-元素顯示在父元素的什么位置。

  如:Button控件

    android:layout_gravity 表示button在界面上的位置

    android:gravity表示button上的字在button上的位置。

  可選值[多選時用“|”分開]

    top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

  • top 將對象放在其容器的頂部,不改變其大小.
  • bottom 將對象放在其容器的底部,不改變其大小.
  • left將對象放在其容器的左側,不改變其大小.
  • right將對象放在其容器的右側,不改變其大小.
  • center_vertical 將對象縱向居中,不改變其大小.

  垂直對齊方式:垂直方向上居中對齊。

    fill_vertical 必要的時候增加對象的縱向大小,以完全充滿其容器. 垂直方向填充

    center_horizontal 將對象橫向居中,不改變其大小水平對齊方式:水平方向上居中對齊

    fill_horizontal 必要的時候增加對象的橫向大小,以完全充滿其容器. 水平方向填充

    center 將對象橫縱居中,不改變其大小.

    fill 必要的時候增加對象的橫縱向大小,以完全充滿其容器.

    clip_vertical 附加選項,用于按照容器的邊來剪切對象的頂部和/或底部的內容. 剪切基于其縱向對齊設置:頂部對齊時,剪切底部;底部對齊時剪切頂部;除此之外剪切頂部和底部.垂直方向裁剪

  clip_horizontal 附加選項,用于按照容器的邊來剪切對象的左側和/或右側的內容. 剪切基于其橫向對齊設置:左側對齊時,剪切右側;右側對齊時剪切左側;除此之外剪切左 ? ? ?側和右側.水平方向裁剪

  例子

  TextView要讓文本垂直/水平居中顯示,有兩種情況需要考慮:

  1、layout_width/layout_height為wrap_content,此時要讓TextView在父控件上居中顯示,必須設置layout_gravity=”center”。

  2、layout_width/layout_height為fill_parent,此時由于TextView已占據父窗體所有空間,必須設置gravity=”center”。

二、相對布局

  相對布局的子控件會根據它們所設置的參照控件和參數進行相對布局。參照控件可以是父控件,也可以是其它子控件,但是被參照的控件必須要在參照它的控件之前定義

  常用屬性:

  RelativeLayout用到的一些重要的屬性:
  第一類:屬性值為true或false
    android:layout_centerHrizontal 水平居中
    android:layout_centerVertical 垂直居中
    android:layout_centerInparent 相對于父元素完全居中
    android:layout_alignParentBottom 貼緊父元素的下邊緣
    android:layout_alignParentLeft 貼緊父元素的左邊緣
    android:layout_alignParentRight 貼緊父元素的右邊緣
    android:layout_alignParentTop 貼緊父元素的上邊緣
    android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
  第二類:屬性值必須為id的引用名“@id/id-name”
    android:layout_below 在某元素的下方
    android:layout_above 在某元素的的上方
    android:layout_toLeftOf 在某元素的左邊
    android:layout_toRightOf 在某元素的右邊
    android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
    android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
    android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
    android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
  第三類:屬性值為具體的像素值,如30dip,40px
    android:layout_marginBottom 離某元素底邊緣的距離
    android:layout_marginLeft 離某元素左邊緣的距離
    android:layout_marginRight 離某元素右邊緣的距離
    android:layout_marginTop 離某元素上邊緣的距離
    EditText的android:hint
  設置EditText為空時輸入框內的提示信息。
    android:gravity
    android:gravity屬性是對該view 內容的限定.比如一個button 上面的text. 你可以設置該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則 ? ? ? ? ? ? ? ? ? ? ? ?button上面的文字靠右
    android:layout_gravity
    android:layout_gravity是用來設置該view相對與起父view 的位置.比如一個button 在linearlayout里,你想把該button放在靠左、靠右等位置就可以通過該屬性設置. ? ? ? ? ? ? ? ? ? ? ? ?以button為例,android:layout_gravity="right"則button靠右
    android:layout_alignParentRight
  使當前控件的右端和父控件的右端對齊。這里屬性值只能為true或false,默認false。
    android:scaleType:
    android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType / android:scaleType值的意義區別:
    CENTER /center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
    CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等于或大于View的長(寬)
    CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等于或小于View的長/寬
    FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示
    FIT_END / fitEnd 把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置
    FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置
    FIT_XY / fitXY 把圖片 不按比例擴大/縮小到View的大小顯示
    MATRIX / matrix 用矩陣來繪制,動態縮小放大圖片來顯示。
    ** 要注意一點,Drawable文件夾里面的圖片命名是不能大寫的。

例子:

復制代碼
 1 <?xml version="1.0" encoding="utf-8"?>2 <?xml version="1.0" encoding="utf-8"?>3 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"4 android:layout_width="fill_parent"5 android:layout_height="fill_parent"6 >7 <AnalogClock8     android:id="@+id/aclock"9     android:layout_width="wrap_content"
10     android:layout_height="wrap_content"
11     android:layout_centerInParent="true" />
12 <DigitalClock
13     android:id="@+id/dclock"
14     android:layout_width="wrap_content"
15     android:layout_height="wrap_content"
16     android:layout_below="@id/aclock"
17     android:layout_alignLeft="@id/aclock"
18     android:layout_marginLeft="40px" />
19 <TextView
20     android:layout_width="wrap_content"
21     android:layout_height="wrap_content"
22     android:text="當前時間:"
23     android:layout_toLeftOf="@id/dclock"
24     android:layout_alignTop="@id/aclock"/>
25 </RelativeLayout>
復制代碼

?

三、幀布局(FrameLayout)

  幀布局,又叫框架布局,是五大布局中最簡單的一個布局,在這個布局中,整個界面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放于這塊區域的左上角,并且后面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。顯示效果如下,第一個TextView被第二個TextView完全遮擋,第三個TextView遮擋了第二個TextView的部分位置

?

常用屬性:

  android:foreground:設置改幀布局容器的前景圖像

  android:foregroundGravity:設置前景圖像顯示的位置

?

例子:

  

復制代碼
 1 <?xml version="1.0" encoding="utf-8"?>2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent" android:layout_height="fill_parent">4 <LinearLayout android:id="@+id/linearLayout1"5 android:layout_height="match_parent"6 android:layout_width="match_parent">7 <Button android:text="Button"8 android:id="@+id/button1"9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"></Button>
11 </LinearLayout>
12 <LinearLayout android:layout_width="match_parent"
13 android:id="@+id/linearLayout3"
14 android:layout_height="match_parent"
15 android:gravity="bottom|right">
16 <Button android:text="Button"
17 android:id="@+id/button3"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"></Button>
20 </LinearLayout>
21 <LinearLayout android:layout_height="match_parent"
22     android:id="@+id/linearLayout2"
23     android:layout_width="match_parent"
24     android:gravity="right">
25 <Button android:text="Button"
26     android:id="@+id/button2"
27     android:layout_width="wrap_content"
28     android:layout_height="wrap_content"></Button>
29 </LinearLayout>
30 <LinearLayout android:layout_width="match_parent"
31     android:id="@+id/LinearLayout01"
32     android:layout_height="match_parent"
33     android:gravity="bottom|left">
34 <Button android:id="@+id/Button01"
35     android:text="Button"
36     android:layout_width="wrap_content"
37     android:layout_height="wrap_content"></Button>
38 </LinearLayout>
39 </FrameLayout>
復制代碼

?

?

四、絕對布局(AbsoluteLayout)

  絕對布局的子控件需要指定相對于此坐標布局的橫縱坐標值,否則將會像框架布局那樣被排在左上角。手機應用需要適應不同的屏幕大小,而這種布局模型不能自適應屏幕尺寸大小,所以應用的相對較少。

?

常用屬性:

  1.控制大小

  android:layout_width:組件高度

  android:layout_height:組件高度

  2.控制位置

  android:layout_x:設置組件的X坐標

  android:layout_y:設置組件的Y坐標

?由于該布局應用低,不同的手機,屏幕大小不同,顯示的布局也不一樣,所以我就部深入了解了

五、表格布局(TableLayout)

  表格布局模型以行列的形式管理子控件,每一行為一個TableRow的對象,當然也可以是一個View的對象。TableRow可以添加子控件,每添加一個為一列。

?

常用屬性:

  

  android:collapseColumns:將TableLayout里面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。?????????????

  android:stretchColumns:設置指定的列為可伸展的列,以填滿剩下的多余空白空間,若有多列需要設置為可伸展,請用逗號將需要伸展的列序號隔開。????????????????

  android:shrinkColumns:設置指定的列為可收縮的列。當可收縮的列太寬(內容過多)不會被擠出屏幕。當需要設置多列為可收縮時,將列序號用逗號隔開。

?  列元素(Button)屬性:(奇怪的是button 里面沒有android:layout_column 和android:layout_span兩個屬性,寫進去無反應,還不知道為什么)

  android:layout_colum:設置該控件在TableRow中指定的列

  android:layout_span:設置該控件所跨越的列數

?

例子:

?

  

復制代碼
 1 <?xml version="1.0" encoding="utf-8"?>2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"3 android:layout_width="fill_parent"4 android:layout_height="fill_parent"5 android:stretchColumns="1">6 <TableRow>7 <TextView8 android:layout_column="1"9 android:padding="3dip" android:text="Row1"/>
10 <TextView
11 android:text="1"
12 android:gravity="right"
13 android:padding="3dip" />
14 </TableRow>
15 <View
16 android:layout_height="2dip"
17 android:background="#FF909090" />
18 <TableRow>
19 <TextView
20 android:text="*"
21 android:padding="3dip" />
22 <TextView
23 android:text="Row12"
24 android:padding="3dip" />
25 <TextView
26 android:text="2"
27 android:gravity="right"
28 android:padding="3dip" />
29 </TableRow>
30 <View
31 android:layout_height="2dip"
32 android:background="#FF909090" />
33 <TableRow>
34 <TextView
35 android:layout_column="1"
36 android:text="Row13"
37 android:padding="3dip" />
38 </TableRow>
39 </TableLayout>

轉載于:https://www.cnblogs.com/hxb2016/p/6090286.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/270450.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/270450.shtml
英文地址,請注明出處:http://en.pswp.cn/news/270450.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

前端:uniapp封裝網絡請求筆記

uniapp作為開發移動端的前端框架&#xff0c;目前國內是非常流行的&#xff0c;使用HbuilderX開發工具基于uniapp框架開發的系統可以方便的轉換為小程序、APP等移動端程序&#xff0c;大大降低了移動開發的成本。網絡請求更是每個前端項目必備的技術&#xff0c;所以有必要進行…

java中instanceof使用詳細介紹

instanceof是Java語言中的一個二元運算符&#xff0c;它的作用是&#xff1a;判斷一個引用類型變量所指向的對象是否是一個類(或接口、抽象類、父類)的實例&#xff0c;即它左邊的對象是否是它右邊的類的實例&#xff0c;該運算符返回boolean類型的數據。 boolean result obj…

javaWEB總結(9):自定義HttpServlet

前言:我們知道 MyHttpServlet是MyGenericServlet的子類&#xff0c;MyHttpServlet會繼承父類的方法&#xff0c;可是卻很少去追問MyHttpServlet中的doGet方法和doPost方法是如何進行判斷的&#xff0c;本文主要做一個小例子&#xff0c;進一步理解HttpServlet。有些代碼前文多次…

硬件知識:u-boot和bootloader的區別

目錄 1、Bootloader 2、uboot 2.1.硬件管理 2.2.能夠完成鏡像燒錄&#xff08;刷機&#xff09; 2.3.uboot的“生命周期” 2.4.uboot要提供命令式shell界面 3、bootloader 與 uboot的區別 嵌入式軟件工程師聽說過 u-boot 和 bootloader&#xff0c;但很多工程師依然不知道他們到…

Java中局部變量必須初始化

Java中有兩種變量&#xff0c;局部變量和成員變量。 成員變量可以不進行初始化&#xff0c;虛擬機也會確保它有一個默認的值。 局部變量不能這樣做&#xff0c;我們必須對它進行賦值&#xff0c;才能使用它。

php 直播服務器搭建,基于Nginx搭建RTMP/HLS視頻直播服務器

1、Nginx環境搭建(基于lnmp環境)//下載并安裝lnmp環境wget -c http://soft.vpser.net/lnmp/lnmp1.3.tar.gz && tar zxf lnmp1.3.tar.gz && cd lnmp1.3 && ./install.sh lnmp安裝完成安裝完成后訪問服務器地址會出現如下界面lnmpPS&#xff1a;安裝時生…

辦公:office辦公軟件Excel表格的打印技巧

很多新手使用辦公軟件過程中&#xff0c;對于Excel的打印出現了諸多問題&#xff0c;今天我們就一起來看看表格打印的幾個技巧&#xff01; 打印預覽時網格線怎么不見了&#xff1f; 如何調整打印區域的頁邊距&#xff1f; 如何設置單色打印&#xff1f; ...... 這些打印時遇到…

vuejs 中如何優雅的獲取 Input 值

http://www.sunzhongwei.com/how-to-get-input-value-in-vuejs轉載于:https://www.cnblogs.com/benpaodexiaopangzi/p/6093275.html

線程池和線程詳細教程

1. 線程池的概念&#xff1a; 線程池就是首先創建一些線程&#xff0c;它們的集合稱為線程池。使用線程池可以很好地提高性能&#xff0c;線程池在系統啟動時即創建大量空閑的線程&#xff0c;程序將一個任務傳給線程池&#xff0c;線程池就會啟動一條線程來執行這個任務&#…

虛擬主機 php .htacess,LiteSpeed添加虛擬主機+支持htaccess圖文教程

上次給大家簡單介紹了Debian下手動安裝LiteSpeedMySQLPHP的教程(點擊查看)&#xff0c;但是這個教程還沒完&#xff0c;想要使用litespeed還要進入后臺進行設置&#xff0c;包括添加虛擬主機和.htaccess偽靜態的支持&#xff0c;本文就繼續這個話題給大家詳細做個圖文教程吧~為…

前端:uniapp封裝組件用法筆記

大家在做前端項目開發的時候&#xff0c;經常會遇到公用的一些頁面&#xff0c;比如搜索、列表、商品詳情卡片、評論列表等。為了提高開發效率、使代碼看起來更加簡潔&#xff0c;這個時候封裝相應的組件是最好的解決方案。今天小編給大家介紹一下如何在uniapp中封裝組件&#…

Angular的工作原理

首先上一小段代碼&#xff08;index.html&#xff09;&#xff0c;結合代碼我們來看看&#xff0c;angular一步一步都做了些什么。 <!doctype html> <html ng-app><head><script src"angular.js"></script></head><body>&…

php中等腰金字塔挖空,php 用for循環做,金字塔,菱形,空三角

echo "金字塔 style1";for($i1;$i<9;$i){for($k0;$kecho "*";}echo "";}echo "金字塔 style2";for($c5;$c>0;$c--){for($c10;$c1echo "*";}echo "";}echo "金字塔 style3";for($a0;$a<11;$a){…

網絡知識:四個網絡命令ping、arp、tracert、route的用法介紹

網絡相關的從業人員&#xff0c;都需要面對檢測和解決網絡故障的各種問題&#xff0c;實際案例中因為網絡導致的故障也是最多的&#xff0c;今天我們和大家一起來學習一下解決網絡故障時使用最多的四個網絡命令。希望對大家以后的實際工作中的故障排除起到作用。 1、Ping命令的…

jQuery擲骰子

網上找的jQuery擲骰子效果&#xff0c;測試兼容IE7及以上瀏覽器&#xff0c;IE6沒有測試 js代碼如下&#xff1a; 1 $(function(){2 var dice $("#dice");3 dice.click(function(){4 $(".wrap").append("<div iddice_mask><…

電腦知識:臺式電腦如何使用無線網上網

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…

oracle的function的語法,Oracle function語法

2018-3-30 遇到需要使用SQL方法拼接字符串的情況&#xff0c;就研究了一下SQL簡單的方法應用--定義入參數[參數名 in 參數類型]create or replace function p_gettype(se_type in varchar2)--定義返回類型return varchar2isv_calling_type varchar2(45);v_called_type varchar2…

進程動態優先級調度

簡單的進程優先級動態調度 cup運行&#xff1a; 每執行一次&#xff0c;優先級減一&#xff0c;運行時間減一。 就緒隊列中的進程&#xff1a;每次按優先級降序排序&#xff08;優先級越大越優先執行&#xff09;&#xff0c;若優先級相等再按時間升序排序&#xff08;時間越小…

電腦維修:如何給筆記本電腦升級內存條

??作者主頁&#xff1a;IT技術分享社區 ??作者簡介&#xff1a;大家好,我是IT技術分享社區的博主&#xff0c;從事C#、Java開發九年&#xff0c;對數據庫、C#、Java、前端、運維、電腦技巧等經驗豐富。 ??個人榮譽&#xff1a; 數據庫領域優質創作者&#x1f3c6;&#x…