編輯:關於 Android 程式設計
RelativeLayout 是 Android 裡最靈活的配置之一,靠著「相對於誰」來定位元件。屬性雖多,但依參照對象分類之後就好記了。
android:layout_above 將本元件的底邊置於指定 ID 元件的上方
android:layout_below 將本元件的頂邊置於指定 ID 元件的下方
android:layout_toLeftOf 將本元件的右邊緣對齊指定 ID 元件的左邊緣
android:layout_toRightOf 將本元件的左邊緣對齊指定 ID 元件的右邊緣
android:layout_alignBaseline 將本元件的 baseline 與指定 ID 的 baseline 對齊
android:layout_alignTop 將本元件的頂部邊緣與指定 ID 的頂部邊緣對齊
android:layout_alignBottom 將本元件的底部邊緣與指定 ID 的底部邊緣對齊
android:layout_alignLeft 將本元件的左邊緣與指定 ID 的左邊緣對齊
android:layout_alignRight 將本元件的右邊緣與指定 ID 的右邊緣對齊
android:layout_alignParentTop 為 true 時,本元件頂部與父容器頂部對齊
android:layout_alignParentBottom 為 true 時,本元件底部與父容器底部對齊
android:layout_alignParentLeft 為 true 時,本元件左側與父容器左側對齊
android:layout_alignParentRight 為 true 時,本元件右側與父容器右側對齊
android:layout_centerHorizontal 為 true 時水平置中
android:layout_centerVertical 為 true 時垂直置中
android:layout_centerInParent 為 true 時置於父容器正中央
android:layout_marginTop 向上偏移
android:layout_marginBottom 向下偏移
android:layout_marginLeft 向左偏移
android:layout_marginRight 向右偏移
android:layout_below = "@id/***"
android:layout_alignBaseline = "@id/***"
android:layout_alignParentTop = true
android:layout_marginLeft = "10px"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label">
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK" />
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
android:layout_below="@id/label" 把目前元件放在 id 為 label 的元件下方。
android:layout_alignParentRight="true" 讓目前元件的右端與父容器右端對齊;此屬性值只能是 true 或 false,預設 false。
android:layout_marginLeft="10dip" 讓目前元件左側留出指定間距。
android:layout_toLeftOf="@id/ok" 把目前元件放在 id 為 ok 的元件左邊。
android:layout_alignTop="@id/ok" 讓目前元件與 id 元件的頂端對齊。
第一類:值為 true 或 false
android:layout_centerHorizontal
android:layout_centerVertical
android:layout_centerInParent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
第二類:值必須是 id 引用,寫成 "@id/id-name"
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop(以及 alignBottom / alignLeft / alignRight / alignBaseline)
第三類:值為具體尺寸,如 30dip、40px
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
使用 RelativeLayout 時務必注意參照順序:被參照的元件若宣告在後面,會導致對齊失效。另一個常見地雷是「互相參照」——A 說自己在 B 下方,B 又說自己在 A 下方,這會讓配置計算陷入循環,系統會直接拋出錯誤。避開這兩點,RelativeLayout 的層級壓縮能力其實優於多層 LinearLayout 嵌套。
Android 高仿微信實時聊天:基於百度雲推送實現
一直在仿微信界面,今天終於有幸利用百度雲推送,把微信聊天也仿了出來。 首先特別感謝 weidi1989 分享的《Android 之基於百度雲推送 IM》,節省了很多事。本
RecyclerView.Adapter 的優化與封裝實踐
說實話,比起敲代碼,我更喜歡看書或看博文,到現在好久都沒練手了。後來在簡書上看到別人對 adapter 的封裝和優化,嘿,還真的挺不錯的,adapter 真的簡潔了不少。
大陸地區更新 Android SDK 卡關?三種可行的解法
在網路環境受限的地區,Google 與 Android 開發者網站常常無法直接連線,做 Android 開發的第一道關卡往往不是程式碼,而是「SDK 下載不下來」。 不過
自定義 View 實作雷達式搜索動畫:擴散圓與旋轉游標
藍牙搜索、附近熱點掃描這類功能,如果只丟一個轉圈圈會顯得很敷衍。做一個有擴散波紋、有中心圖示緩慢旋轉的雷達效果,成本低但質感提升明顯。本文用一個自定義 View 把整個動