Android Development for Beginners -Building Layouts Part B

前言

Lesson 1中包含三大部分內(nèi)容:

  • 如何根據(jù)需求選擇對應(yīng)的View(展示圖片:ImageView 展示文本:TextView)
  • 如何修改View的樣式(字體顏色金拒、圖片的縮放裁剪)
  • 接下來就來講講這第三小節(jié)的內(nèi)容:如何將多個View按照需求在屏幕上進(jìn)行擺放

引入

市面上每個應(yīng)用的界面都是由許多的View組合而成的(QQ 微信 微博)那么這就涉及到整個布局中的View如何去進(jìn)行擺放兵拢?

ViewGroup

ViewGroup能夠把View擺放在特定的位置(上or下or中……)

通過例子說明:

上次課中的例子都是只對單獨(dú)的某個View作說明

對單獨(dú)的某個View作說明

嘗試著再拷貝一份TextView元素放到下面鲸阻,你會發(fā)現(xiàn)出現(xiàn)了錯誤


錯誤寫法

注意:根View有且僅有一個,想要放置多個View時根View必須為一個ViewGroup

ViewGroup也屬于View的一種,是屏幕上的一塊矩形服爷,ViewGroup包含于View药薯,View包含ViewGroup

如圖

圖中可以看到ViewGroup同樣有寬高、背景顏色等屬性 不難理解因?yàn)閂iewGroup屬于View
當(dāng)然ViewGroup通常都是透明的即用戶看不見的
可以把ViewGroup理解為View的容器

一個View包含著其他的View咪橙,前者為父View夕膀,后者叫子View,后者之間稱兄弟View

美侦? 看圖提問 产舞?

提問

答案在文章底部公布

到目前為止,我們已經(jīng)知道了 想要放置多個View在屏幕上就必須把它們放到ViewGroup里

ViewGroup的類型

ViewGroup有很多種菠剩,每種ViewGroup都有其包含視圖的規(guī)則,先介紹兩種最最最常用的ViewGroup

LinearLayout(線性布局)

LinearLayout就是一個ViewGroup
規(guī)則:子視圖稱線性排列故稱線性布局易猫,所謂線性即水平豎直

線性布局

理論結(jié)束,看代碼如何編寫

線性布局代碼

ViewGroup使用單獨(dú)的關(guān)閉標(biāo)簽具壮,子視圖書寫在ViewGroup的開始標(biāo)簽和結(jié)束標(biāo)簽之間
LinearLayout的方向通過其屬性: android:orientation 指定准颓,
其值有二: vertical豎直方向、horizontal水平方向

特別注意:如果你不知道orientation屬性是干嘛的棺妓,那你會怎么辦攘已?問別人? 千萬別怜跑,你應(yīng)該直接去百度或谷歌一下样勃,肯定有你要的答案吠勘。 這里我想告訴大家的是要學(xué)會通過搜索引擎去解決問題。這種事以后還多著呢峡眶!

照著上圖給你的代碼去XMLV練習(xí)一下剧防,嘗試著增加一些子視圖和更改它們的排列方向。

命名空間:XML命名空間聲明辫樱,使用命名空間來指定所有的android屬性峭拘,為什么使用android:前綴就是這個原因,特指示android系統(tǒng)自帶的屬性狮暑,其實(shí)前面也說過我們是可以自定義屬性的鸡挠,自定義屬性時,前綴就是我們自定義的某個文本而非android了搬男。 android: 是用來區(qū)分這些都是Android系統(tǒng)自帶的屬性
所以務(wù)必在根視圖的開始標(biāo)簽中加上XML命名空間的聲明

LinearLayout的寬高

  • xxdp(固定寬高 xx具體指某個數(shù)值)
  • 當(dāng)內(nèi)容的大小大于視圖所設(shè)置的寬高時宵凌,會顯示不全(被切掉一部分)
  • wrap_content (包裹內(nèi)容)
  • 視圖大小 == 內(nèi)容大小
  • match_parent(匹配父布局)
  • 無論視圖寬高為多少,視圖與父布局均等寬/高


線性布局寬高
![](http://upload-images.jianshu.io/upload_images/1869092-200cb5e22a7eb4ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

注意一種場景: 當(dāng)前線性布局方向?yàn)閔orizontal(豎直方向)止后,若有其中一個子視圖的高度為match_parent瞎惫,那么這個子視圖將把它下面的子視圖都頂出父布局之外(不可見)自己占據(jù)父布局剩下的高度,同理寬度亦然译株。

線性布局 - 屏幕適配

運(yùn)行Android系統(tǒng)的設(shè)備千千萬萬瓜喇,設(shè)備的屏幕大小、屏幕分辨率各式各樣歉糜。
由此就會產(chǎn)生一個問題: 應(yīng)用在不同設(shè)備上所顯示的效果千差萬別乘寒!
就如下圖所示,分別在縱向放置匪补、橫向放置伞辛、大屏設(shè)備上顯示的效果會不盡人意,三個TextView都擠到左上角了夯缺,這對于使用大屏設(shè)備的用戶來說是尤為不人性化 也就是所說的用戶體驗(yàn)極差蚤氏。



需求 如果想要讓這三個TextView無論在什么設(shè)備上都平均在屏幕的上中下的位置需要怎么做呢?

嘗試用我們學(xué)過的知識:

  • 每個TextView使用固定的高度均分屏幕大杏欢怠竿滨?
  • 只能針對某一個分辨率的屏幕,不能適配所有的屏幕大小
  • 使用wrap_content捏境?
  • 包裹內(nèi)容就如上圖所示
  • 使用match_parent于游?
  • 匹配父元素會將剩余的兄弟師傅擠出父布局之外(無法看見)

以上做法都不能實(shí)現(xiàn)適配所有的設(shè)備,腫么辦垫言?問人贰剥? 不! 問人之前先百度/谷歌?昶怠0龀伞柱告!
或者可以直接去安卓官方文檔查找LinearLayout的屬性 相信你能自己解決!

Layout Weight (布局權(quán)重)

布局權(quán)重:在線性布局中給各個子視圖按比例分配空間笑陈,注意:僅在LinearLayout中有權(quán)重的概念

權(quán)重的使用
步驟一:

  • 當(dāng)父布局(線性布局)的方向?yàn)樗椒较颍╤orizontal)時
    • android:layout_width="0dp" 把寬度設(shè)置為0dp
  • 當(dāng)父布局(線性布局)的方向?yàn)樨Q直方向(vertical)時
    • android:layout_height="0dp" 把高度設(shè)置為0dp

步驟二:

  • android:layout_weight="xx" 給每個子視圖分配權(quán)重 xx為某個數(shù)值

權(quán)重越大,分配的空間越大


實(shí)現(xiàn)均分只需分配相等的權(quán)重即可

![Uploading QQ截圖20160401172435_550530.png . . .]


同理大家可以自己去實(shí)現(xiàn)水平方向的均分

來些應(yīng)用例子


微信輸入框布局

線性布局的東西到此結(jié)束葵袭!

RelativeLayout(相對布局)

相對布局相對于線性布局而言涵妥,更加靈活,方便坡锡。

規(guī)則:子視圖與父布局(父視圖)相對排列蓬网,或 子視圖之間相對排列

代碼如何編寫?

子視圖與父布局(父視圖)相對排列

      android:layout_alignParentTop="true"            <!--與父布局的上邊對齊-->
      android:layout_alignParentBottom="true"         <!--與父布局的下邊對齊-->
      android:layout_alignParentLeft="true"           <!--與父布局的左邊對齊-->
      android:layout_alignParentRight="true"          <!--與父布局的右邊對齊-->
      android:layout_centerInParent="true"            <!--(水平豎直都)居中于父布局-->
      android:layout_centerHorizontal="true"          <!--水平居中于父布局-->
      android:layout_centerVertical="true"            <!--豎直居中于父布局-->
            
      <!--上述屬性值默認(rèn)為false-->

示例

根據(jù)圖片上的文字位置鹉勒,想想要用哪些個屬性實(shí)現(xiàn)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--默認(rèn)在左上角-->
    <TextView
        android:text="左上"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="右上"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="左中"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="右中"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="左下"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="右下"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="上中"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="下中"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
    <TextView
        android:text="正中"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge" />
</RelativeLayout>

子視圖之間相對排列

  android:layout_toLeftOf="@id/xxx"       <!--在id為xxx的視圖的左側(cè)-->
  android:layout_toRightOf="@id/xxx"      <!--在id為xxx的視圖的右側(cè)-->
  android:layout_below="@id/xxx"          <!--在id為xxx的視圖的下方-->
  android:layout_above="@id/xxx"          <!--在id為xxx的視圖的上方-->

  android:layout_alignTop="xxx"           <!--上邊緣與id為xxx的視圖對齊-->
  android:layout_alignBottom="xxx"        <!--下邊緣與id為xxx的視圖對齊-->
  android:layout_alignLeft="@id/xxx"      <!--左邊緣與id為xxx的視圖對齊-->
  android:layout_alignRight="xxx"         <!--右邊緣與id為xxx的視圖對齊-->
  
  

說明: 每個視圖都可以設(shè)置id屬性作為唯一標(biāo)識

  android:id="@+id/xxx"              <!--@+id  表示聲明一個id屬性-->

  android:layout_toLeftOf="@id/xxx"  <!--@id   引用某個已聲明的id屬性對應(yīng)的視圖帆锋,在id為xxx的視圖左邊-->

示例

相對布局示例效果圖
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_up"
        android:text="上"
        android:layout_above="@id/btn_center"
        android:layout_alignLeft="@id/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
    <Button
        android:id="@+id/btn_center"
        android:text="中"
        android:layout_toRightOf="@id/btn_left"
        android:layout_above="@id/btn_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
    <Button
        android:id="@+id/btn_down"
        android:text="下"
        android:layout_alignLeft="@id/btn_center"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
    <Button
        android:id="@+id/btn_left"
        android:text="左"
        android:layout_alignTop="@id/btn_center"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
    <Button
        android:id="@+id/btn_right"
        android:text="右"
        android:layout_toRightOf="@id/btn_center"
        android:layout_alignTop="@id/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>

    <Button
        android:id="@+id/btn_hand"
        android:text="拳"
        android:layout_alignBottom="@id/btn_foot"
        android:layout_toLeftOf="@id/btn_foot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
    <Button
        android:id="@+id/btn_foot"
        android:text="腳"
        android:layout_alignBottom="@id/btn_center"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"/>
</RelativeLayout>

代碼結(jié)合上面的屬性說明與效果圖對比學(xué)習(xí)。

再來看一個常見的例子禽额,QQ好友列表項(xiàng)的布局



動手寫一下布局吧锯厢,相信你可以的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

   <ImageView
       android:id="@+id/iv_icon"
       android:src="@mipmap/ic_launcher"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_name"
        android:text="樹懶"
        android:layout_toRightOf="@id/iv_icon"
        android:textAppearance="?android:textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_text"
        android:text="[在線] 時間沖淡了我的咖啡"
        android:textColor="@android:color/darker_gray"
        android:layout_toRightOf="@id/iv_icon"
        android:layout_below="@id/tv_name"
        android:textAppearance="?android:textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_signal"
        android:text="4G"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/darker_gray"
        android:textAppearance="?android:textAppearanceMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

代碼顯示效果如下圖

是不是感覺View都擠到一起了很難看 - -!

不足: 這些View都緊挨著父布局的邊緣脯倒,這樣顯示給用戶的感覺非常的不好实辑。

怎么樣給視圖周圍留出一些空白呢?

Padding & Margin (內(nèi)邊距 & 外邊距)

用圖說話

內(nèi)外邊距的區(qū)別

根據(jù)上圖可以清楚的看到Padding 和Margin之間的區(qū)別

  • Padding 內(nèi)邊距:View與View的內(nèi)容之間的邊距
  • Margin 外邊距 : 整個View與父布局之間的邊距

在實(shí)際開發(fā)中由于視圖的背景一般都為透明藻丢,所以二者之間看起來并無差別

android:padding="xxdp"              <!--上下左右的內(nèi)邊距為xxdp-->
android:paddingTop="xxdp"           <!--上邊的內(nèi)邊距為xxdp-->
android:paddingBottom="xxdp"        <!--下邊的內(nèi)邊距為xxdp-->
android:paddingLeft="xxdp"          <!--左邊的內(nèi)邊距為xxdp-->
android:paddingRight="xxdp"         <!--右邊的內(nèi)邊距為xxdp-->

android:layout_margin="xxdp"          <!--上下左右的外邊距為xxdp-->
android:layout_marginTop="xxdp"       <!--上邊的外邊距為xxdp-->
android:layout_marginBottom="xxdp"    <!--下邊的外邊距為xxdp-->
android:layout_marginLeft="xxdp"      <!--左邊的外邊距為xxdp-->
android:layout_marginRight="xxdp"     <!--右邊的外邊距為xxdp-->

據(jù)此修改上面寫的QQ好友列表項(xiàng)的代碼如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:padding="8dp">     <!-- 給整個布局添加內(nèi)邊距 -->

  <ImageView
      android:id="@+id/iv_icon"
      android:src="@mipmap/ic_launcher"
      android:layout_marginRight="8dp"  
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />    <!-- 給圖片添加右邊的外邊距 -->
   <TextView
       android:id="@+id/tv_name"
       android:text="樹懶"
       android:layout_toRightOf="@id/iv_icon"
       android:textAppearance="?android:textAppearanceLarge"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <TextView
       android:id="@+id/tv_text"
       android:text="[在線] 時間沖淡了我的咖啡"
       android:textColor="@android:color/darker_gray"
       android:layout_toRightOf="@id/iv_icon"
       android:layout_below="@id/tv_name"
       android:textAppearance="?android:textAppearanceMedium"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <TextView
       android:id="@+id/tv_signal"
       android:text="4G"
       android:layout_alignParentRight="true"
       android:textColor="@android:color/darker_gray"
       android:textAppearance="?android:textAppearanceMedium"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
</RelativeLayout>
效果圖

至于邊距應(yīng)該設(shè)置為多少合適剪撬,參照Meterial Design 設(shè)計規(guī)范:
http://wiki.jikexueyuan.com/project/material-design/

規(guī)范中推薦為8dp的倍數(shù)。

END

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末悠反,一起剝皮案震驚了整個濱河市残黑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌斋否,老刑警劉巖梨水,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異茵臭,居然都是意外死亡冰木,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進(jìn)店門笼恰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來踊沸,“玉大人,你說我怎么就攤上這事社证”乒辏” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵追葡,是天一觀的道長腺律。 經(jīng)常有香客問我奕短,道長,這世上最難降的妖魔是什么匀钧? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任翎碑,我火速辦了婚禮,結(jié)果婚禮上之斯,老公的妹妹穿的比我還像新娘日杈。我一直安慰自己,他們只是感情好佑刷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布莉擒。 她就那樣靜靜地躺著,像睡著了一般瘫絮。 火紅的嫁衣襯著肌膚如雪涨冀。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天麦萤,我揣著相機(jī)與錄音鹿鳖,去河邊找鬼。 笑死壮莹,一個胖子當(dāng)著我的面吹牛栓辜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播垛孔,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼藕甩,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了周荐?” 一聲冷哼從身側(cè)響起狭莱,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎概作,沒想到半個月后腋妙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡讯榕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年骤素,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片愚屁。...
    茶點(diǎn)故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡济竹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出霎槐,到底是詐尸還是另有隱情送浊,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布丘跌,位于F島的核電站袭景,受9級特大地震影響唁桩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜耸棒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一荒澡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧与殃,春花似錦单山、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鸵赫。三九已至衣屏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辩棒,已是汗流浹背狼忱。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留一睁,地道東北人钻弄。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像者吁,于是被迫代替她去往敵國和親窘俺。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容