Android零基礎入門第31節:幾乎不用但要瞭解的AbsoluteLayout絕對布局

來源:互聯網
上載者:User

標籤:world   height   安裝   常式   widget   aec   res   養成   com   

原文:Android零基礎入門第31節:幾乎不用但要瞭解的AbsoluteLayout絕對布局

前面幾期基本學習了Android開發中常用的四種布局,之所以把AbsoluteLayout放在後面來學習,是由於在實際開發中很少使用,而且在高版本中已經注釋待刪除不建議使用,那麼本期學習的目的就是簡單瞭解有這樣一種布局,如果碰到至少知道是怎麼回事。

 

 一、認識AbsoluteLayout

絕對布局需要通過指定x、y座標來控制每一個控制項的位置,放入該布局的控制項需要通過android:layout_x和android:layout_y 兩個屬性指定其準確的座標值,並顯示在螢幕上。

需要注意的是當使用AbsoluteLayout作為版面配置容器時,版面配置容器不再管理子組件的位置和大小,都需要開發人員自己控制。使用絕對布局時,每個子組件都可指定如下兩個XML屬性。

  • layout_x:指定該子組件的X座標。

  • layout_y:指定該子組件的Y座標。

     

 二、樣本

接下來通過一個簡單的樣本程式來學習AbsoluteLayout的使用用法。

同樣使用WidgetSample工程,繼續使用app/main/res/layout/目錄下的activity_main.xml檔案,在其中填充如下程式碼片段:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="0dp"        android:layout_y="0dp"        android:text="按鈕1" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="40dp"        android:layout_y="40dp"        android:text="按鈕2" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="80dp"        android:layout_y="80dp"        android:text="按鈕3" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_x="120dp"        android:layout_y="120dp"        android:text="按鈕4" /></AbsoluteLayout>

運行程式,可以看到所示介面效果:

需要注意的是,理論上絕對布局可以完成任何的布局設計,但是實際的工程應用中不提倡使用這種布局。因為使用這種布局不但需要精確計算每個組件的大小,而且當應用程式運行在不同螢幕的手機上產生的效果也不相同,因此,一般不推薦使用絕對布局。一般可以用LinearLayout的weight權重+ RelativeLayout來構建我們的介面。

 

 

今天就先到這裡,如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論群,共同成長!

此文章著作權為公眾號分享達人秀(ShareExpert)——鑫鱻所有,若需轉載請聯絡作者授權,特此聲明!

 

往期總結分享:

Android零基礎入門第1節:Android的前世今生

Android零基礎入門第2節:Android 系統架構和應用組件那些事

Android零基礎入門第3節:帶你一起來聊一聊Android開發環境

Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招

Android零基礎入門第5節:善用ADT Bundle, 輕鬆邂逅女神

Android零基礎入門第6節:配置最佳化SDK Manager, 正式約會女神

Android零基礎入門第7節:搞定Android模擬器,開啟甜蜜之旅

Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點

Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發

Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio

Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程

Android零基礎入門第12節:熟悉Android Studio介面,開始裝逼賣萌

Android零基礎入門第13節:Android Studio配置最佳化,打造開發利器

Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代

Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航

Android零基礎入門第16節:Android使用者介面開發概述

Android零基礎入門第17節:TextView屬性和方法大全

Android零基礎入門第18節:EditText的屬性和使用方法

Android零基礎入門第19節:Button使用詳解

Android零基礎入門第20節:CheckBox和RadioButton使用大全

Android零基礎入門第21節:ToggleButton和Switch使用大全

Android零基礎入門第22節:ImageView的屬性和方法大全

Android零基礎入門第23節:ImageButton和ZoomButton使用大全

Android零基礎入門第24節:自訂View簡單使用,打造屬於你的控制項

Android零基礎入門第25節:簡單且最常用的LinearLayout線性布局

Android零基礎入門第26節:兩種對齊,layout_gravity和gravity大不同

Android零基礎入門第27節:正確使用padding和margin

Android零基礎入門第28節:輕鬆掌握RelativeLayout相對布局

Android零基礎入門第29節:善用TableLayout表格版面配置

Android零基礎入門第30節:兩分鐘掌握FrameLayout幀布局

Android零基礎入門第31節:幾乎不用但要瞭解的AbsoluteLayout絕對布局

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.