編輯:關於 Android 程式設計
按鈕明明設了對齊屬性卻沒有效果,多半是把 gravity 與 layout_gravity 搞混了。這兩個屬性只差幾個字,作用對象卻完全不同。
只有 LinearLayout 有這個屬性。horizontal 讓子元素由左到右水平排列,vertical 讓子元素由上到下垂直排列。
android:gravity:指定本元素的內容(子元素)相對於自己的對齊方式。android:layout_gravity:指定本元素相對於父容器的對齊方式。一句話記法:gravity 往裡對,layout_gravity 往外擺。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<Button android:text="button01" android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:text="button02" android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
兩個按鈕都會被推到右側對齊。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_gravity="left"
android:text="button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button
android:layout_gravity="right"
android:text="button02"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
兩個按鈕各自依自己的 layout_gravity 靠左右兩側。要注意的是,layout_gravity 只在 LinearLayout 與 FrameLayout 這類支援的父容器中有效,放在 RelativeLayout 裡並不會有任何作用。
FrameLayout 相當於螢幕上預留的一塊空白區域,你可以在其中放入單一物件(例如一張要顯示的圖片)。它的特性是:所有子元素都固定在左上角,無法為個別子元素指定位置;後加入的子元素會覆蓋在前一個之上,將其部分或全部遮住(除非後者是透明的)。這種特性正好適合做圖片疊圖、進度遮罩或浮層。
用 XML 定義版面,再由 Activity 的 setContentView() 依定義建立真正的物件——XML 是設計圖,setContentView 是生產線。而 FrameLayout 可用的屬性來自三處:繼承而來的、嵌套類(LayoutParams)定義的、以及自身定義的。幾個常用者:
android:id:繼承自 android.view.View,為元件提供唯一識別碼,之後可用 findViewById() 取得。android:layout_width/android:layout_height:定義在 FrameLayout.LayoutParams 中,取值為 fill_parent(在該軸向撐滿父容器)或 wrap_content(依內容大小收縮)。android:background:背景。android:foreground:前景,會畫在子元素之上,常用來放遮罩或按壓回饋。其餘屬性可查閱官方文件;釐清「誰對齊誰」之後,多層嵌套的版面就不容易再出現「怎麼設都不動」的狀況。
ES 檔案瀏覽器遠端管理功能怎麼用
ES 檔案瀏覽器是 Android 上常見的檔案管理器。除了基本的檔案操作,它還能讓其他裝置透過 WiFi 遠端連進手機的檔案系統——不用拉線,電腦就能直接讀寫手機裡的東
[Android & Java] 設計模式筆記:觀察者模式
觀察者,就像一個人對很多事物都感興趣——音樂、電子產品、遊戲、股票等等,這些事物的任何變化都能引起愛好者的注意並時刻關注。在程式碼中,我們也可以用同樣的思路來設計一些有趣
深度剖析 Android ImageView 及其擴展技巧
版本:1.0 日期:2014.6.11(2014.6.12 修訂) 版權:© 2014 kince(轉載請註明出處) ImageVi
小米「請勿遮擋橙色區域」怎麼解?防誤觸與距離感測器校正
小米手機出現請勿遮擋橙色區域,通常是防誤觸或距離感測器作用。可先到設定、鎖屏和密碼,關閉防誤觸模式。 若仍異常,可在撥號盤輸入工程模式,進入距離感應器測試,將手指靠近感測