編輯:關於 Android 程式設計
先來看個需求,見下圖:


這是一個軟件的登錄界面:初始是第一張圖的樣子,當軟鍵盤彈出後變成第二張圖的樣子。因為登錄界面有用戶名、密碼和登錄按鈕,若不這麼處理,軟鍵盤彈出後就會遮住登錄按鈕。(其實也可以把內容放進 ScrollView,監聽軟鍵盤彈出後滾到底部、隱藏後滾回頂部,同樣可行。)
最簡單粗暴的做法,是多加幾個冗餘的 View,根據軟鍵盤狀態隱藏不需要的、顯示需要的——但這樣實在太「挫」了。於是我想起了前兩年研究的 RelativeLayout:它子控件的布局都是相對位置,只需在軟鍵盤彈出/隱藏時改變相關的位置規則就行了。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
tools:context="${packageName}.${activityClass}" >
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageView
android:id="@+id/logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/hello_world"
android:textSize="20sp" />
</RelativeLayout>
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/container"
android:layout_margin="16dp"
android:hint="Input sth."
tools:ignore="HardcodedText" />
</RelativeLayout>
軟鍵盤的彈出與隱藏,用 OnGlobalLayoutListener 監聽實現;同時給 Activity 加上 android:windowSoftInputMode="stateHidden|adjustResize",這樣初始時軟鍵盤不顯示,彈出時布局會被 Resize。
public class MainActivity extends Activity {
private View root; // 最外層布局
private View logo; // Logo 圖標
private View label; // Logo 附近的文字
private int rootBottom = Integer.MIN_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
logo = findViewById(R.id.logo);
label = findViewById(R.id.label);
root.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
root.getGlobalVisibleRect(r);
// 進入 Activity 時會布局,第一次先記錄軟鍵盤未彈出時底部的位置
if (rootBottom == Integer.MIN_VALUE) {
rootBottom = r.bottom;
return;
}
// adjustResize,軟鍵盤彈出後高度變小
if (r.bottom < rootBottom) {
RelativeLayout.LayoutParams lp = (LayoutParams) logo.getLayoutParams();
// 若 Logo 已非水平居中,說明是本次改動導致的再次布局,忽略以免死循環
if (lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] != 0) {
lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 0); // 取消水平居中
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // 左對齊
// 縮小 Logo 為 1/2
int height = logo.getHeight();
int width = logo.getWidth();
lp.width = width / 2;
lp.height = height / 2;
logo.setLayoutParams(lp);
RelativeLayout.LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.addRule(RelativeLayout.CENTER_HORIZONTAL, 0); // 取消水平居中
labelParams.addRule(RelativeLayout.BELOW, 0); // 取消顯示到 logo 下方
labelParams.addRule(RelativeLayout.RIGHT_OF, R.id.logo); // 顯示到 Logo 右方
labelParams.addRule(RelativeLayout.CENTER_VERTICAL); // 垂直居中
label.setLayoutParams(labelParams);
}
} else {
// 軟鍵盤收起或初始化時
RelativeLayout.LayoutParams lp = (LayoutParams) logo.getLayoutParams();
if (lp.getRules()[RelativeLayout.CENTER_HORIZONTAL] == 0) {
lp.addRule(RelativeLayout.CENTER_HORIZONTAL); // 居中 Logo
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
// 還原 Logo 為原來大小
int height = logo.getHeight();
int width = logo.getWidth();
lp.width = width * 2;
lp.height = height * 2;
logo.setLayoutParams(lp);
RelativeLayout.LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.addRule(RelativeLayout.CENTER_HORIZONTAL); // 水平居中
labelParams.addRule(RelativeLayout.BELOW, R.id.logo); // 顯示到 Logo 下面
labelParams.addRule(RelativeLayout.RIGHT_OF, 0); // 取消顯示到 Logo 右面
labelParams.addRule(RelativeLayout.CENTER_VERTICAL, 0); // 取消垂直居中
label.setLayoutParams(labelParams);
}
}
}
});
}
}
原理是這樣的:Activity 啟動時也會進行 Layout,此時用 rootBottom 記錄最外層布局底部的初始位置;此後當軟鍵盤彈出,布局被壓縮,再取同一個 View 底部的位置,如果比 rootBottom 小,說明軟鍵盤彈出了;大於或等於則說明軟鍵盤已隱藏。
onGlobalLayout,而且通常會調用兩次,第二次會進入 else 分支,要記濾掉;onGlobalLayout,此時縮放 Logo、改變位置的操作又會引起再次 onGlobalLayout,需要把隨之而來的調用過濾掉,否則會陷入無限循環。代碼中的過濾條件正是為此而設。以 else 分支為例:Activity 啟動時進入 else,此時 Logo 是水平居中狀態,會跳過裡面的 if,處理掉了第一種情況;而當因軟鍵盤收起進入 else 時,Logo 已因上一輪的 if 變為左上角顯示,於會進入 if 重新改回水平居中,由於修改了大小與位置又會再次進入 onGlobalLayout,仍是 else,但此時已設為水平居中,不會再進 if——一個條件判斷就化解了上述兩個難題。
RelativeLayout 中每個子控件所應用的規則,都是用數組保存的:
public static final int TRUE = -1;
public void addRule(int verb) {
mRules[verb] = TRUE;
mInitialRules[verb] = TRUE;
mRulesChanged = true;
}
public void addRule(int verb, int anchor) {
mRules[verb] = anchor;
mInitialRules[verb] = anchor;
mRulesChanged = true;
}
以某規則的索引為下標,值就是對應的 anchor:若相對於另一個子控件,值就是那個子控件的 ID;若相對於父控件,值就是 TRUE(即 -1);若沒有應用某規則,值就是 0。可以看到,removeRule 就是把相應位置的值改為 0:
public void removeRule(int verb) {
mRules[verb] = 0;
mInitialRules[verb] = 0;
mRulesChanged = true;
}
removeRule 是 API 17 才加入的方法,為了在 API 17 之前也能使用,可以採用它的等價寫法——像上面例子那樣用 addRule(verb, 0) 來達到同樣的效果。
自製電子書翻頁時的 Fragment 崩潰排查記:Duplicate id 問題全記錄
做原生看書頁面的時候,書籍內容頁需要放一個輪播圖組件。我在網上找到一個現成的 CycleViewPager 別人封裝好的开源實作,直接下載來用,結果踩了一個頗有教育意義的
Android 自定義狀態欄顏色,讓它與標題欄保持一致
用 iOS 時會注意到,狀態欄的顏色總能與 App 的標題欄融為一體,質感很好。這種好奇驅使我去查:Android 行不行?答案是——4.4 之前不行,4.4 之後可以了
Android 頁面跳轉詳解:Intent 與 Bundle 的幾種用法
在 Android 中,每個屏幕通常對應一個 Activity,各屏幕彼此獨立,而屏幕之間的跳轉則透過 Intent 來實現。本文以猜拳小遊戲為例,系統梳理頁面跳轉的三種
Cordova 5.3.1(PhoneGap)混合開發環境搭建完整指南
引言:認識 Cordova先簡單介紹 Cordova 的來歷。Cordova 的前身叫 PhoneGap,自被 Adobe 收購後交由 Apache 管理,並將核心功能開