編輯:關於 Android 程式設計
在看 API 時經常會遇到 Start 對齊、End 對齊。Start 對齊主要是為了在不同 textDirection(文字排列方向)下更好地對齊:當 textDirection 為 LTR(從左到右)時,Start 對齊等同左對齊;當為 RTL(從右到左)時,Start 對齊則等同右對齊,End 同理。
使用 gravity 可實現 View 內文字的對齊方式。官方解釋如下:
android:gravity:Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.
意思是:當文字比 View 小時,指定如何依 View 的 x、y 軸對齊。屬性值可以是下列一個或多個,多個時以 '|' 隔開:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal、start、end。其中既有上下左右這種基本型,也有像 left|bottom 這樣的組合型。
左對齊:
<TextView
android:layout_width="100dip"
android:layout_height="60dip"
android:layout_marginTop="10dip"
android:background="#ffff33"
android:gravity="left"
android:text="left" />
左下角對齊:
<TextView
android:layout_width="100dip"
android:layout_height="60dip"
android:layout_marginTop="10dip"
android:background="#ffff33"
android:gravity="left|bottom"
android:text="leftbottom" />
官方解釋:
gravity:Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
意思是:規定一個對象如何依 x、y 軸在自己的範圍內擺放內容。可用值同上,多個以 '|' 隔開。
居右對齊:
<LinearLayout
android:layout_width="match_parent"
android:background="#0000ff"
android:layout_height="0dip"
android:gravity="right"
android:layout_weight="1" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="right" />
</LinearLayout>
居左下角對齊:
<LinearLayout
android:layout_width="match_parent"
android:background="#ffff33"
android:layout_height="0dip"
android:layout_marginTop="10dip"
android:gravity="left|bottom"
android:layout_weight="1" >
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="left_bottom" />
</LinearLayout>
RelativeLayout 主要用於相對位置對齊,常用屬性如下:
layout_above 將該元件底部設到指定 ID 對象上方;layout_below 設到下方;layout_alignBaseline 與指定 ID 對象基線對齊;layout_alignBottom 底部與指定 ID 對象底部對齊;layout_alignLeft、layout_alignRight、layout_alignTop 分別與指定 ID 對象的左、右、頂部對齊;layout_alignStart 開始(左)與指定對象開始位置對齊;layout_alignEnd 結束(右)與指定對象結束位置對齊;layout_toLeftOf 將該元件右邊放到指定 ID 對象左邊;layout_toStartOf 類似;layout_toRightOf 將該元件左邊放到指定 ID 對象右邊;layout_centerHorizontal 水平居中;layout_centerInParent 置於父對象中央;layout_centerVertical 垂直居中;layout_alignParentLeft 左邊與父對象左邊對齊;layout_alignParentRight 右邊與父對象右邊對齊;layout_alignParentStart、layout_alignParentTop、layout_alignParentEnd、layout_alignParentBottom 則分別與父對象對應邊對齊。
例子一:把一個 button 放到另一個 button 右邊。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標准物1" />
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btnStand"
android:text="right->標准物1" />
</RelativeLayout>
例子二:把一個 button 放到另一個 button 的右下角。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnStand2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="標准物2" />
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="@id/btnStand2"
android:layout_toRightOf="@id/btnStand2"
android:text="right_bottom->標准物2" />
</RelativeLayout>
SEAndroid 安全機制框架深度解析:內核空間與用戶空間的協同運作
Android 系統建立在 Linux 之上。針對傳統 Linux 系統,NSA 開發了 SELinux 這套強制存取控制機制來強化安全性。但 Android 有自己獨特
百度地圖開發實戰:自定義縮放、覆蓋物與路線規劃
本文整理在專案中常見的百度地圖自定義需求,包含自定義縮放按鈕、自定義定位按鈕、文字覆蓋物、覆蓋物縮放效果、只顯示當前屏幕內覆蓋物、駕車路線規劃與最近站點搜尋。 自定義縮放
Android 安全審計工具 MobiSecLab 上手實測
分析 Android 應用時,本地端有許多工具可以勝任。本文介紹的是另一條路——線上化的惡意軟體檢測與靜態分析平台 MobiSecLab。國內相關介紹不多,但實際使用量並
微信名字怎麼改?順便教你怎麼在名字裡放表情符號
一個微信名字用久了總會想換,換個稱呼也換個心情。改名字很簡單,想在名字裡塞進表情符號稍微多繞一步,但都不難。修改微信名字1. 開啟微信並登入帳號,點擊導覽列最右側的「我」