1. 程式人生 > >安卓中的五大布局詳解

安卓中的五大布局詳解

原文轉自:http://blog.csdn.net/llping2011/article/details/9992941?utm_source=tuicool&utm_medium=referral

我們知道Android系統應用程式一般是由多個Activity組成,而這些Activity以檢視的形式展現在我們面前,檢視都是由一個一個的元件構成的。元件就是我們常見的Button、TextEdit等等。那麼我們平時看到的Android手機中那些漂亮的介面是怎麼顯示出來的呢?這就要用到Android的佈局管理器了,網上有人比喻的很好:佈局好比是建築裡的框架,元件按照佈局的要求依次排列,就組成了用於看見的漂亮介面了。

      在分析佈局之前,我們首先看看控制元件:Android中任何視覺化的控制元件都是從android.veiw.View繼承而來的,系統提供了兩種方法來設定檢視:第一種也是我們最常用的的使用XML檔案來配置View的相關屬性,然後在程式啟動時系統根據配置檔案來建立相應的View檢視。第二種是我們在程式碼中直接使用相應的類來建立檢視。

     如何使用XML檔案定義檢視:

      每個Android專案的原始碼目錄下都有個res/layout目錄,這個目錄就是用來存放佈局檔案的。佈局檔案一般以對應activity的名字命名,以 .xml 為字尾。在xml中為建立元件時,需要為元件指定id,如:android:id="@+id/名字"系統會自動在gen目錄下建立相應的R資源類變數。 

    如何在程式碼中使用檢視:  

     在程式碼中建立每個Activity時,一般是在onCreate()方法中,呼叫setContentView()來載入指定的xml佈局檔案,然後就可以通過findViewById()來獲得在佈局檔案中建立的相應id的控制元件了,如Button等。

 如:

[html] view plain copy  print?
  1. private Button btnSndMag;  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  // 載入main.xml佈局檔案  
  5.     btnSndMag = (Button)this.findViewById(R.id.btnSndMag); // 通過id找到對於的Button元件  
  6.     ....  
  7. }  

  下面我們來介紹Android系統中為我們提供的五大布局:LinearLayout(線性佈局)、FrameLayout(單幀佈局)、AbsoluteLayout(絕對佈局)、TablelLayout(表格佈局)、RelativeLayout(相對佈局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。這些佈局都可以巢狀使用。

(1)LinearLayout 線性佈局

  線性佈局是按照水平或垂直的順序將子元素(可以是控制元件或佈局)依次按照順序排列,每一個元素都位於前面一個元素之後。線性佈局分為兩種:水平方向和垂直方向的佈局。分別通過屬性android:orientation="vertical" 和 android:orientation="horizontal"來設定。

 android:layout_weight 表示子元素佔據的空間大小的比例,有人說這個值大小和佔據空間成正比,有人說反比。我在實際應用中設定和網上資料顯示的剛好相反,這個問題後面會專門寫一篇文章來分析。現在我們只需要按照正比例來設定就可以。 

例如下面我們實現一個如圖所示的簡易計算器介面:


程式碼:

[html] view plain copy  print?
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:orientation="vertical"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:background="#FFFFFF"
  7.     tools:context=".MainActivity">
  8.     // 這裡第一行顯示標籤為一個水平佈局  
  9.     <LinearLayout
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:orientation="horizontal">
  13.         <EditText
  14.             android:id="@+id/msg"
  15.             android:inputType="number"
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content"
  18.             android:text="">
  19.         </EditText>
  20.     </LinearLayout>
  21.     // 第二行為 mc m+ m- mr 四個Button構成一個水平佈局  
  22.     <LinearLayout
  23.         android:layout_width="match_parent"
  24.         android:layout_height="wrap_content"
  25.         android:orientation="horizontal">
  26.         <Button
  27.             android:layout_width="match_parent"
  28.             android:layout_height="wrap_content"
  29.             android:text="mc"android:layout_weight="1">
  30.         </Button>
  31.         <Button
  32.             android:layout_width="match_parent"
  33.             android:layout_height="wrap_content"
  34.             android:text="m+"android:layout_weight="1">
  35.         </Button>
  36.         <Button
  37.             android:layout_width="match_parent"
  38.             android:layout_height="wrap_content"
  39.             android:text="m-"android:layout_weight="1">
  40.         </Button>
  41.         <Button
  42.             android:layout_width="match_parent"
  43.             android:layout_height="wrap_content"
  44.             android:text="mr"android:layout_weight="1">
  45.         </Button>
  46.     </LinearLayout>
  47.     // 同上 C +/-  / * 四個Button構成一個水平佈局  
  48.       <LinearLayout
  49.           android:layout_width="match_parent"
  50.           android:layout_height="wrap_content"
  51.           android:orientation="horizontal">
  52.           <Button
  53.               android:layout_width="match_parent"
  54.               android:layout_height="wrap_content"
  55.               android:layout_weight="1"
  56.               android:text="C">
  57.           </Button>
  58.           <Button
  59.               android:layout_width="match_parent"
  60.               android:layout_height="wrap_content"
  61.               android:layout_weight="1"
  62.               android:text="+/-">
  63.           </Button>
  64.           <Button
  65.               android:layout_width="match_parent"
  66.               android:layout_height="wrap_content"
  67.               android:layout_weight="1"
  68.               android:text="/">
  69.           </Button>
  70.           <Button
  71. 相關推薦

    五大

    原文轉自:http://blog.csdn.net/llping2011/article/details/9992941?utm_source=tuicool&utm_medium=referral 我們知道Android系統應用程式一般是由多個Acti

    Notification通知的

         在訊息通知時,我們經常用到兩個元件Toast和Notification。特別是重要的和需要長時間顯示的資訊,用Notification就最 合適不過了。當有訊息通知時,狀態列會顯示通知的圖示和文字,通過下拉狀態列,就可以看到通知資訊了,Android這一創新性的U

    Android系統五大Layout

      我們知道Android系統應用程式一般是由多個Activity組成,而這些Activity以檢視的形式展現在我們面前,檢視都是由一個一個的元件構成的。元件就是我們常見的Button、TextEdit等等。那麼我們平時看到的Android手機中那些漂亮的介面是怎麼顯示出來的

    簡單介紹五大

    Android對用五大布局物件,它們分別是FrameLayout(框架佈局),LinearLayout (線性佈局),AbsoluteLayout(絕對佈局),RelativeLayout(相對佈局),TableLayout(表格佈局). FrameLayout: Fr

    Android Relative Layout 相對

    沒有 horizon 水平 gravity 邊界線 w3c 水平居中 ntb ole 思維導圖可在幕布找到 1. 基礎 如果在相對布局裏,控件沒有指明相對位置,則默認都是在相對布局的左上角: <TextView android:layout_width=

    【Android】Android六種

    spec rec 默認 bottom ron ado 居中 右下角 控制 這篇就對LinearLayout、RelativeLayout、自定義ViewGroup、FrameLayout、TableLayout、AbsoluteLayout六種布局進行詳細的講解。 1

    Android入門——六大

    本博文對LinearLayout、RelativeLayout、自定義ViewGroup、FrameLayout、TableLayout、AbsoluteLayout六種佈局進行詳細的講解。 一.LinearLayout佈局 <?xml version="1.0" encodin

    開發——AndroidManifest.xml配置

    AndroidManifest.xml配置檔案稱為清單檔案,對於Android應用開發來說是非常重要的基礎知識,在學習中總結該配置檔案中重點的用法,以便日後查閱。下面是一個標準的AndroidManifest.xml檔案樣例。 xml version="1.0" encod

    6.0許可權申請

    安卓6.0的一大變化就是對於許可權的限制,首次安裝應用時會產生一個許可權請求列表,需要使用者手動逐個確認每個許可權,應用才能獲取該許可權。而在6.0之前預設開啟的,因此會產生一些應用會讀取使用者的一些隱私資訊,影響使用者體驗。本文根據實際專案開發經驗,簡述基於安

    Android.mk 檔案語法

    0. Android.mk簡介: Android.mk檔案用來告知NDK Build 系統關於Source的資訊。 Android.mk將是GNU Makefile的一部分,且將被Build System解析一次或多次。 所以,請儘量少的在Android.mk中宣告

    (淘寶無限適配)手機端rem

    招聘 ont 原則 logs note 都是 接下來 範圍 border 從網易與淘寶的font-size思考前端設計稿與工作流 本文結合自己對網易與淘寶移動端首頁html元素上的font-size這個屬性的思考與學習,討論html5設計稿尺寸以及前端與設計之間協作流程的

    基礎知識回顧------Android五大

    Android中的五大布局 在android中的佈局有五大類,這五種佈局為別為:LinearLayout(線性佈局),FrameLayout(框架佈局),RelativeLayout(相對佈局),TableLayout(表格佈局),AbsoluteLayout

    的訊息迴圈機制Handler及Looper

    我們知道安卓中的UI執行緒不是執行緒安全的,我們不能在UI執行緒中進行耗時操作,通常我們的做法是開啟一個子執行緒在子執行緒中處理耗時操作,但是安卓規定不允許在子執行緒中進行UI的更新操作,通常我們會通過Handler機制來完成該功能,即當子執行緒中耗時操作完成後,在子執行緒

    SQLlite在的基本和簡單使用

    一、基礎介紹 1.SQLite 是一個程序內的庫,是一種輕量級的、自給自足的、無伺服器的、無需配置的,事務性的SQL資料庫引擎.和他其他的資料庫一樣,SQLite引擎不是一個獨立的程序,可以按應用程式需求進行靜態或動態連線。SQLite可以直接訪問其儲存檔案。

    學習筆記之———五大

    首先介紹一下安卓的五大布局分別是什麼? 線性佈局(LinearLayout) 相對佈局(RelativeLayout) 幀佈局(FrameLayout) 表格佈局(TableLayout) 絕對佈局

    基礎:Activity基礎、五大

    Activity: Activity是Android系統中的四大元件之一,可以用於顯示View。它是一種可以包含使用者介面的元件,主要用於和使用者進行互動。 Activity是有生命週期的,每個Activity在其生命週期中最多可能會有四種狀態: Ac

    Android開發之五大

    為了適應各式各樣的介面風格,Android系統提供了5種佈局,這5種佈局分別是: LinearLayout(線性佈局) TableLayout(表格佈局) RelativeLayout(相對佈局) AbsoluteLayout(絕對佈局) FrameLayout(框架佈局)

    contentDescription屬性的作用

    在寫Android的XML佈局檔案時,在ImageView或ImageButton中需要新增 android:contentDescription="" 可能大家覺得這個屬性並沒有多大用處,其實這個屬

    五大

     FrameLayout(框架佈局) LinearLayout (線性佈局) AbsoluteLayout(絕對佈局) RelativeLayout(相對佈局) TableLayout(表格

    控制元件懸浮在某個控制元件之上 Android五大方式——相對佈局(RelativeLayout)屬性

    如圖所示,想要實現搜尋框在輪播圖上面,不管滑動與否,這個組合控制元件都在上面 1.就要用到RelativeLayout,相對於父元素定位,而且這個搜尋框的組合控制元件必須要放在所有控制元件下面,在使用android:layout_alignParentTop="true"