跳到主要內容

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

超好用的無限免費網頁空間,無廣告,無流量限制

大家好,我是奶綠茶 今天來教大家如何申請一個無限免費速度又快的網頁空間 1 首先到  https://github.com/ 申請帳號(一直下一步,下一步,下一步) 2 到你的個人頁,切換上方的 tab 到 Repositories, 按下右鍵的 new 3 Repository name 一定要是這樣的格式 username.github.io 我的 github 網址是 github.com/milkmidi 那就要輸入 milkmidi.github.io 選擇 public, 這樣別人才看的到 private 有其他用途, 而且要付費才能使用 完成後按下 Create repository 5 安裝 SourceTree github 並不支援 FTP 或是網頁上傳,一定要透過指令碼 在這我們選用有圖型介面的軟體,方便大家學習 https://www.sourcetreeapp.com/ 下載並安裝 啟動後登入你的 github 帳號 6 clone 你的 github io 專案 右上角有個 Clone or download 點選後 複製 https 連結(不要選到 ssh ) 7 將 https 的連結貼到 SourceTree 8 上傳 html 到本機 github.io 資料夾,放一個 index.html 切換到 SourceTree, 這時會看到 Unstaged files 的欄位 選擇 Stage All 9 git 要求每次的 Commit, 都一定要打說明文字(好習慣) 輸入完成後,按下右邊的 Commit 10 發佈(Push),這樣就完成啦 可以到你的 http://milkmidi.github.io/ 去查看檔案有沒有出來 其他 Commit 可以想像是做一個記錄,你可以很多的 Commit 最後再一次 Push 上去 github 原本是給程式設計師用的版本控管服務 免費版提供無限空間讓你放檔案,但一定要是 public 想要有私有的 Project ,就只能付費 github.io 只能放靜態檔案,php, aspx 服務並不支援。 祝大家學習愉快 轉載請註明出處 奶綠的 github.io S