跳到主要內容

Android LinearLayout

大家好, 我是奶綠茶
LinearLayout:
子元素依序排成一列(或是一行), 就像是 Flex 裡的 HBox(或VBox)
我們在一個 LinearLayout (horizontal)裡,放置三個 Button , 其寬、高皆為 wrap_content
語法:
<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#ff0000" >        
   <Button
     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A_btn"          
        />
        <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"         
         android:text="B_btn" 
        />
        <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="C_btn" 
        />
</LinearLayout> 
結果:




如果現在想要讓三個 Button 能夠自動置滿整個 LinearLayout 寬度的話
可以使用 android:layout_weight 屬性, 指的就是"重要度"

layout_weight 屬性 在 LinearLyaout 和該子物件裡, 會有二種不同的結果
先看比較好瞭解的部份, layout_weight 寫在子物件裡

件條:當子物件的 layout_width="wrap_content" 時 (android:orientation="horizontal")
設定三個 Button 的 weight 值
A_btn weight = 2
B_btn weight = 1
C_btn weight = 0 (default value)

這樣指的意思就是
三個 Button 先依自已的寬度排好, 如上圖片所示
結果依剩下的空間, 來做分配
A_btn weight 為 2 , 較為重要, 所以分到剩下空間的 2/3
B_btn weight 為 1 , 分到剩下空間的 1 /3
C_btn weight 為 0 , 所以寬度不變
結論就是 weight 為大, 重要度也越大
修正一下 xml layout
<LinearLayout
  android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000" >
        
        <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:text="A_btn"          
        />
        <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"         
         android:text="B_btn" 
        />
        <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="C_btn" 
        />
     </LinearLayout> 
結果




第二種情況
layout_weight 寫在 LinearLayout
假設現在有二個 LinearLayout 分邊叫 R , G
<LinearLayout
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#ff0000" >
 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="2"
  android:text="A_btn"          
 />
 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1"         
  android:text="B_btn" 
 />
 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="C_btn" 
 />
</LinearLayout>  



<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#00ff00" >
 <EditText 
        android:text="abcx" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView 
        android:text="13456" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout> 
結果, 你會看到只有紅色的 LinearLayout
因寫他怖滿了整個 layout




加入 weight 屬性
R LinearLayout weight = 2
G LinearLayout weight = 1
結果是
畫面的 2/3 分給 weight 為1 的 G
畫面的 1/3 分給 weight 為2 的 R
剛好倒過來
結論就是 weight 越大, 比重越小
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:background="#ff0000" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="A_btn"          
 />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"         
        android:text="B_btn" 
 />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C_btn" 
        />
</LinearLayout>  
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#00ff00" >
    <EditText 
        android:text="abcx" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView 
        android:text="13456" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout> 

留言

這個網誌中的熱門文章

奶綠茶新書上市_密技公怖_程式碼下載

奶綠茶第一本 Flash 書籍上市啦 博客來書籍館 Flash 3D 特效宅急便 - 商業範例隨學隨 把我對 Flash 的研究與熱愛, 全寫在其中 本書的前幾章, 特別安排 ActionScript3.0 的許多範例 好讓你一步步瞭解 as2 和 as3 的差別 再安排製作動畫不可缺少的 Tweener 類別和一些常用的數學動畫公式 最後當然就來到本書的重點 PV3D 啦 從 PV3D 的原理, 基本架構一點一點的介紹 再透過許多實用的範例, 從中瞭解 PV3D 製作的小技巧 有任何書籍相關的問題, 也請不吝指教 感謝一路走來, 幫助過我的前輩們, 邦邦、Ticore、神魂、Maso、Erin 旗標出版社 小編 Bready 米蘭數位所有夥伴 米蘭數位ActionScript Team:Jason,Ash,Mark 本書大鋼: 1.ActionScript3.0概論 分析AS3的概念與語法, 從基本的滑鼠事件、外部載入、氣泡事件介紹起 適合熟悉AS2, 想轉戰AS3的人員。 2.程式碼製作動畫 使用簡單的數學公式, 套用到程式裡, 脫離死版的影格動畫。 3.製作動畫的好夥伴:Tweener 除了套用數學公式外, Tweener類別也能加速我們開發出更多的動畫效果, 不需辛辛苦苦的找公式。 4.自定類別 AS3的精神, 是在於強大的物件導向, 當然也要熟悉類別的寫法與使用。 5.Flash3D - Papervison3D 如果在Flash玩轉3D效果, PV3D是最好學, 最快上手的3D Flash Engine, 從簡單的3D概念, PV3D類別, 事件等。 6 漫天飛舞的Paper3DWorld效果 7 迷你旋轉木馬式秀圖效果 8 最經典! CoverFlow 圖像展示效果 9 天旋地轉 TiltViewer效果 10 FlatWall3D電視牆效果 11 經典旋轉木馬 Carousel3D 秀圖效果 12 光源材質展示與簡易型的方塊彈跳效果 13 載入外部3D模型與動畫 14 超立體旋轉九宮格 Box3DWorld 15 空間感與透明感兼具的 SpaceGallery 16 螺旋式 SpiralCarouse 影像展示 17 Flash10 3D功能 18 Flash效...

奶綠茶photoGalleryV3.5

奶小茶的photoGalleryV3.5原始檔 2007/12/30 更新小Bug,請重新下載 線上Demo: http://milkmidi.com/photogallery/galleryv3.html 1.支援SWFAddress,讓FLASH可以有上一頁下一頁和每張獨立網址的功能 2.圖片路徑圖說經由XML來設定 3.背景依據顯示圖片,自動變化成最接近的顏色 4.非商業用途,歡迎使用並修改(如果覺得我製作的好,也煩請加入我的logo) 5.可結合後端伺服器 00.art 相本集資料夾 com 奶綠茶的Class檔 swfaddress SWFAddress資料夾 swfobject swfobject資料夾 XML galleryV3.fla 原始檔 index.html 主檔html SWFAddress.as SWFaddressEvent.as Source Code Download 解壓密碼:回覆文章,即可看見Password

FLARToolKit

什麼是FLARToolKit 簡單來說, 就是透過Webcam, 將虛擬的物件與真實的世界結合, 呈現在使用者的畫面 Demo影片: http://www.youtube.com/watch?v=TW6_X9qBeds&feature=related Youtube有許多相關的影片。 一開始的版本並不是Flash, 後來日本的程式設計師 Saqoosha 將其改寫成Flash版 並搭配PV3D。 你可以下載其原始碼來研究, 程式碼都是公開的。 http://www.libspark.org/wiki/saqoosha/FLARToolKit/en http://saqoosha.net/flartoolkit/start-up-guide/ 要玩這個技術, 需要具備幾個點: 1.熟悉FlashActionScript3.0 , 特別是類別的部份。 2.略懂PV3D的架構, 因為裡面是用到PV3D的核心Engine。 3.需要有一台Webcam, 4.開始玩樂吧。 即然跟Flash有關, 而且也是PV3D, 奶小茶我當然是不會錯過 使用的是官網的Focus 3D模型車和FlarToolKit製作的Demo, 原始碼官網都有提供喔。 PV3D的相關課程, 可以參考 飛肯學苑 奶老師所開設的PV3D班。 奶小茶Demo的原始碼按我下載