編輯:關於 Android 程式設計
效果是多個圖層依次疊加,像畫一幅畫:先放一張背景 ImageView,再往上疊其他元素。
如果想讓三個按鈕分別在水平方向左對齊、居中、右對齊,可以給每個子 View 單獨設置 layout_gravity:
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="btn1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="btn3" />
</FrameLayout>

要點在於 FrameLayout 的所有子 View 默認都從左上角開始擺放,全靠 gravity 把它們推向不同位置,因此各元素之間不存在相互影響。
靠 android:orientation 決定排列方向。若要在水平或垂直方向上按比例分配空間,則給子 View 設置 layout_weight,剩餘空間會按權重劃分。
允許子 View 相對於父容器或其他兄弟 View 定位,是層級最淺、性能較好的選擇之一。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_toRightOf="@+id/btn1"
android:text="btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn2"
android:layout_toRightOf="@+id/btn2"
android:text="btn3" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn2"
android:layout_toLeftOf="@+id/btn2"
android:text="btn4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn2"
android:layout_toRightOf="@+id/btn2"
android:text="btn5" />
</RelativeLayout>
常用屬性可以歸成兩類:一組是 layout_above / below / leftOf / rightOf,相對於兄弟定位;另一組是 layout_alignParentTop / Bottom / CenterInParent 等,相對於父容器定位。注意被引用的 View 必須先出現在布局中,或者用 @+id/ 提前聲明。
通過 LayoutParams 實現:
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 1) {
i = 0;
lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
} else {
i = 1;
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
}
btn.setLayoutParams(lp);
}
});
關鍵是 setLayoutParams() 之後會觸發重新布局,位置即刻生效。
用 GradientDrawable,構造時傳入漸變方向與顏色數組即可:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] col = {Color.BLUE, Color.BLACK};
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, col);
getWindow().setBackgroundDrawable(gd);
}
除了代碼方式,也可以寫成 XML 放在 res/drawable/ 下再用 android:background="@drawable/xxx" 引用,這種做法更易於維護和調色:
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:startColor="#0000FF"
android:endColor="#000000"
android:type="linear" />
</shape>
注意 XML 中的 angle 必須是 45 的倍數,且 0 度指向東方,與代碼裡的 Orientation 枚舉方向相反,這一點常讓人困惑。
淺談 Android 自定義鎖屏頁:從劃屏解鎖到指紋識別
一、為什麼需要自定義鎖屏頁鎖屏作為一種從黑白屏時代就存在的功能,至今仍發揮著巨大作用,尤其到了觸屏時代,鎖屏的功用更是被發揮到了極致。多少人曾在無聊時每隔幾分鐘劃開鎖屏再
Android Studio 導入第三方庫的完整流程與常見踩坑
前言多數人踏入 Android 開發時,都會選擇 Google 官方的 Android Studio。由於它與早期的 Eclipse 差異不小,入門時遇到的問題也格外多,
Android 新控件 TabLayout:最省事實作選項卡切換
一、概述 TabLayout 是 2015 年 Google I/O 大會上隨 Android Support Design Library 一起發布的新組件,用來全面支
Android 插件化:apkplugdemo 源碼閱讀指南(10)
閱讀本節前,建議先了解 apkplug 基礎教程。本教學基於 apkplug V1.6.8 版本編寫,最新開發方式請以官網為準。可下載最新的 apkplugdemo 源碼