Android布局大全

  Android的界面是有布局和組件協同完成的,布局好比是建筑里的框架,而組件則相當于建筑里的磚瓦。組件按照布局的要求依次排列,就組成了用戶所看見的界面。

? ? ??

? ? ? 所有的布局方式都可以歸類為ViewGroup的5個類別,即ViewGroup的5個直接子類。其它的一些布局都擴展自這5個類。

1.LinearLayout,線性布局方式

  這種布局比較常用,也比較簡單,就是每個元素占一行,當然也可能聲明為橫向排放,也就是每個元素占一列。

  LinearLayout按照垂直或者水平的順序依次排列子元素,每一個子元素都位于前一個元素之后。如果是垂直排列,那么將是一個N行單列的結構,每一行只會有一個元素,而不論這個元素的寬度為多少;如果是水平排列,那么將是一個單行N列的結構。如果搭建兩行兩列的結構,通常的方式是先垂直排列兩個元素,每一個元素里再包含一個LinearLayout進行水平排列。

  LinearLayout中的子元素屬性android:layout_weight生效,它用于描述該子元素在剩余空間中占有的大小比例。加入一行只有一個文本框,那么它的默認值就為0,如果一行中有兩個等長的文本框,那么他們的android:layout_weight值可以是同為1。如果一行中有兩個不等長的文本框,那么他們的android:layout_weight值分別為1和2,那么第一個文本框將占據剩余空間的三分之二,第二個文本框將占據剩余空間中的三分之一。android:layout_weight遵循數值越小,重要度越高的原則。

<?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">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:text="@string/hello"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff654321" android:layout_weight="1" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fffedcba" android:layout_weight="2" android:text="2"/>
</LinearLayout>
</LinearLayout>

2.Relative Layout,相對布局

  RelativeLayout按照各子元素之間的位置關系完成布局。在此布局中的子元素里與位置相關的屬性將生效。例如android:layout_below, ?android:layout_above,?android:layout_centerVertical等。注意在指定位置關系時,引用的ID必須在引用之前,先被定義,否則將出現異常。

  RelativeLayout是Android五大布局結構中最靈活的一種布局結構,比較適合一些復雜界面的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/text_01" android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_alignParentBottom="true" android:text="1"/>
<TextView android:id="@+id/text_02" android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_above="@id/text_01" android:layout_centerHorizontal="true" android:text="2"/>
<TextView android:id="@+id/text_03" android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_toLeftOf="@id/text_02" android:layout_above="@id/text_01" android:text="3"/>
</RelativeLayout>

3.AbsoluteLayout,絕對位置布局

  在此布局中的子元素的android:layout_x和android:layout_y屬性將生效,用于描述該子元素的坐標位置。屏幕左上角為坐標原點(0,0),第一個0代表橫坐標,向右移動此值增大,第二個0代表縱坐標,向下移動,此值增大。在此布局中的子元素可以相互重疊。在實際開發中,通常不采用此布局格式,因為它的界面代碼過于剛性,以至于有可能不能很好的適配各種終端。

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ffffffff" android:gravity="center" android:layout_x="50dp" android:layout_y="50dp" android:text="1"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#ff654321" android:gravity="center" android:layout_x="25dp" android:layout_y="25dp" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:layout_x="125dp" android:layout_y="125dp" android:text="3"/>
</AbsoluteLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
<TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
</FrameLayout>

5.TableLayout,表格布局

  適用于N行N列的布局格式。一個TableLayout由許多TableRow組成,一個TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子類,ablelLayout并不需要明確地聲明包含多少行、多少列,而是通過TableRow,以及其他組件來控制表格的行數和列數,?TableRow也是容器,因此可以向TableRow里面添加其他組件,沒添加一個組件該表格就增加一列。如果想TableLayout里面添加組件,那么該組件就直接占用一行。在表格布局中,列的寬度由該列中最寬的單元格決定,整個表格布局的寬度取決于父容器的寬度(默認是占滿父容器本身)。

  TableLayout繼承了LinearLayout,因此他完全可以支持LinearLayout所支持的全部XML屬性,除此之外TableLayout還支持以下屬性:

?      XML屬性?????????????????????????????????????? 相關用法??????????????????????????????????????????????????? 說明

  1. andriod:collapseColumns?????????? setColumnsCollapsed(int ,boolean)?????? 設置需要隱藏的列的序列號,多個用逗號隔開

  2.android:shrinkColumns????????????? setShrinkAllColumns(boolean)?????????????????設置被收縮的列的序列號,多個用逗號隔開

  3.android:stretchColimns???????????? setSretchAllColumnds(boolean)?????????????? 設置允許被拉伸的列的序列號,多個用逗號隔開

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:background="#fffedcba" android:gravity="center" android:padding="10dp" android:text="3"/>
<TextView android:background="#ff654321" android:gravity="center" android:padding="10dp" android:text="2"/>
<TextView android:background="#ffffffff" android:gravity="center" android:padding="10dp" android:text="1"/>
</TableRow>
</TableLayout>

6.其他布局(隸屬關系請看上圖)

1)列表視圖(List View)

  List View是可滾動的列表。以列表的形式展示具體內容,并且能夠根據數據的長度自適應顯示。

? ? ? 具體應用請看:用法一? http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

         用法二? http://blog.csdn.net/koupoo/article/details/7018727?

2)網格視圖(Grid View)

  Grid View一個ViewGroup以網格顯示它的子視圖(view)元素,即二維的、滾動的網格。

  具體應用查看:http://www.cnblogs.com/linzheng/archive/2011/01/19/1938760.html

3)標簽布局(Tab Layout)

  以標簽的方式顯示它的子視圖元素,就像在Firefox中的一個窗口中顯示多個網頁一樣。為了狂創建一個標簽UI(tabbed UI),需要使用到TabHostTabWidget。TabHost必須是布局的根節點,它包含為了顯示標簽的TabWidget和顯示標簽內容的FrameLayout

  具體應用查看:http://www.cnblogs.com/devinzhang/archive/2012/01/18/2325887.html

?--------------------------------------------------------------------

PS: 歡迎關注公眾號"Devin說",會不定期更新Java相關技術知識。

--------------------------------------------------------------------

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

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

相關文章

java實現加減乘除運算符隨機生成十道題并判斷對錯_2020年Java面試題(3年的工作總結),最全的知識點總結...

這份Java面試題整整花了三個月的時間來整理&#xff0c;都是自己再工作中總結出來&#xff0c;記住多少就寫多少&#xff0c;希望這份資料可以幫助你們&#xff0c;文末有其余部分資料的領取方式.Redis12道面試題1.什么是Redis&#xff1f;答&#xff1a;Remote Dictionary Ser…

Linux MySQL 5.1.62 source install

源碼包官網下載&#xff1a;http://dev.mysql.com/downloads/ MySQL手冊官網下載&#xff1a;http://dev.mysql.com/doc/ 安裝ncurses&#xff1a; 1、tar -zxvf ncurses-5.9.tar.gz 2、cd ncurses-5.9.tar.gz ; ./configure; make clean; make; make install 修改時區…

RDLC 示例 文章 1

GotReportViewer的實例&#xff0c;請在下面的連接中下載&#xff1a; http://files.cnblogs.com/waxdoll/RDLC.rar http://www.databasejournal.com/features/mssql/article.php/3605826/Mastering-OLAP-Reporting--Reporting-with-Analysis-Services-KPIs.htm MSSQL Server …

.NET 中的泛型 101

1.1.1 摘要 圖1 C# 泛型介紹 在接觸泛型之前&#xff0c;我們編程一般都是使用具體類型&#xff08;char, int, string等&#xff09;或自定義類型來定義我們變量&#xff0c;如果我們有一個功能很強的接口&#xff0c;而且我們想把它提取或重構成一個通用的接口&#xff0c;使…

年底了,給想進階的的前端朋友一些福利

2020 年&#xff0c;很多朋友都經歷了一段比較艱難的求職季。年末&#xff0c;“就業寒冬”迎來了一絲暖陽&#xff0c;很多中大型互聯網公司擴大了未來一年的招聘需求。前不久&#xff0c;字節跳動放出了年末要招 1 萬人的消息&#xff0c;騰訊校招規模也將擴張至 5000 人&…

python oa系統_用python把C#操作OA的例子重寫了一下

#手工chrome.exe --remote-debugging-port9222 --user-data-dir"C:\selenum\AutomationProfile"fromselenium import webdriverfromselenium.webdriver.common.by import Byfromselenium.webdriver.support.ui import WebDriverWaitfromselenium.webdriver.chrome.op…

編譯安裝PHP出現configure: error: MySQL configure failed. Please check config.log的解決方法

以下為google的結果&#xff1a; 方案一&#xff1a; 轉載鏈接&#xff1a;http://www.php-oa.com/2008/03/28/php-make.html 好久沒有編譯安裝過php了,為了玩nginx.沒法子,編譯一次來測試.我加的編譯的參數是: # ./configure –prefix/usr/local/php –with-config-file…

[Android]?Android學習手記(二)

1。SDK源碼獲取Git這個版本控制還真是第一次聽到。Linux參考官網&#xff08;需要穿墻&#xff09;的Get source好像比較容易。Windows就比較麻煩了&#xff0c;不能通過repo方式獲取整個projects&#xff0c;只能一個獲取project。不過官網稱“The source is approximentely 2…

關于分區索引與全局索引性能比較的示例

說明&#xff1a;之前使用range分區做出來的效果不明顯&#xff0c;這次使用hash分區。 1、準備工作&#xff1a; ----創建兩張一樣的hash分區表&#xff0c;jacks_part和echos_part------------------ 1 SQL> create table jacks_part (owner varchar2(30),object_id numbe…

Vue Router 4.0 正式發布!煥然一新。

關注若川視野&#xff0c;回復"pdf" 領取資料&#xff0c;回復"加群"&#xff0c;可加群長期交流學習12月8日&#xff0c;Vue Router 4 正式發布穩定版本。在經歷了 14 個 Alpha&#xff0c;13 個 Beta 和 6 個 RC 版本之后&#xff0c;Vue Router v4 閃亮…

實戰Nginx與PHP(FastCGI)的安裝、配置與優化

轉載鏈接&#xff1a;http://ixdba.blog.51cto.com/2895551/806622 一、什么是 FastCGI FastCGI是一個可伸縮地、高速地在HTTP server和動態腳本語言間通信的接口。多數流行的HTTP server都支持FastCGI&#xff0c;包括Apache、Nginx和lighttpd等&#xff0c;同時&#xff0c;…

python在運維自動化的前景_現在學運維自動化python和大數據?

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":7,"count":7}]},"card":[{"des":"阿里云實時計算(Alibaba Cloud Realtime Com…

BOM算最尾階的損耗率 成品直接到料件

假設由B生產為A經過3道工序,各工序的損耗率分別為 C1,C2,C3; 由D生產為B經過1道工序,作業損耗率為C4. 請問在BOM中建立材料的損耗率應該是怎樣的呀? 我的理解是這樣:A的產出B的投入(1-C1)(1-C2)(1-C3)所以B的投入A的產出/(1-C1)(1-C2)(1-C3)所以建A的BOM時,材料B的損耗率為: …

10個前端8個用Vue的,怎么才能在面試中出彩?

大家好&#xff0c;我是若川。現在但凡出去面試&#xff0c;面試官幾乎必問 Vue3.0 。不僅會問一些核心特性&#xff0c;還會問原理層面的問題。比如&#xff1a;?框架層面問題&#xff1a;Vue3.0 新特性 Composition API 與 React.js 中 Hooks 的異同點&#xff1f;?源碼、原…

ASP.NET MVC學習之(5):Html.ActionLink

本文整理了該方法的幾種重載形式&#xff1a; 一 Html.ActionLink("linkText","actionName") 該重載的第一個參數是該鏈接要顯示的文字&#xff0c;第二個參數是對應的控制器的方法&#xff0c;默認控制器為當前頁面的控制器&#xff0c;如果當前頁面的控制…

python qq模塊_常用的Python模塊

目錄1、使用copy模塊來復制>>> class Animal:def _init_(self, species, number_of_legs, color):self.species speciesself.number_of_legs number_of_legsself.color color>>> harry Animal()>>> harry._init_(hippogriff, 6, pink)>>&…

故鄉 | 登高望遠,夜幕降臨

歡迎星標我的公眾號若川視野&#xff0c;回復加群&#xff0c;長期交流學習上周末看了幾集豆瓣評分8.5分劉同同名小說的青春劇《我在未來等你》&#xff0c;讓我回想起自己的高中生活。也想起小時候經常爬到故鄉附近的小山&#xff0c;看夕陽西下。時常和同事開玩笑說&#xff…

CentOS5安裝Nginx1.4+PHP5.5 FastCGI

轉載鏈接&#xff1a;http://blog.csdn.net/staricqxyz/article/details/17012329 yum -y install gcc gcc-c autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2…

FTP服務器的搭建

IIS IIS所提供的FTP功能比較簡單&#xff1a; 用戶依賴于“操作系統用戶”&#xff1b;只提供了全局讀&#xff08;瀏覽和復制&#xff09;、寫&#xff08;刪除、修改、添加&#xff09;功能設置&#xff0c;也就是說所有的讀寫權限都相同&#xff1b;“用戶”與“對應目錄”的…

一份熱乎乎的滴滴前端面經

關注若川視野&#xff0c;回復"pdf" 領取資料&#xff0c;回復"加群"&#xff0c;可加群長期交流學習滴滴前端實習面經滴滴是我投簡歷之后第二家面試的公司&#xff0c; 國慶節前兩三天投的簡歷&#xff0c; 國慶后復工第一天就給我打了電話約一面。那時候…