編輯:關於 Android 程式設計
好一陣子沒更新文章了,最近在忙公司的專案,這也是我參與的第一個正式專案。做完之後才發現,自己累積一年的知識真的還不夠用。想精進,靠得住的路徑其實很樸素:專案、書、影片、部落格,加上最重要的一點——親手做。最近發現慕課網的教學影片免費而且含金量很高,用業餘時間跟著做,收穫不少。以下把這些小 demo 整理出來分享,我在過程中做了一些優化,程式碼與影片略有出入,但功能完整可用。
這是一個模仿微信 5.2.1 版本的介面:


目標功能很單純:
對應的技術選擇:
先來自訂的標題欄:
<!-- top1.xml --> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/topone_bg" android:paddingLeft="12dp" android:paddingRight="12dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_icon" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="微信" android:textColor="#D3D3D3" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_search_icon" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_add_icon" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_more_icon" /> </LinearLayout> </RelativeLayout>
效果如下:

接著是選項卡的部分。三個分頁均分寬度,下方那條藍線就是 indicator:
<!-- top2.xml --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#EEEEEE" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="37dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/ll_chat" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal"> <TextView android:id="@+id/tv_tab_chat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="聊天" android:textColor="#008000" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/tv_tab_discover" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發現" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center"> <TextView android:id="@+id/tv_tab_contacts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通訊錄" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/iv_tab_line" android:layout_width="100dp" android:layout_height="3dp" android:background="@drawable/tabline" /> </LinearLayout>
效果如下:

這裡 indicator 的寬度先寫成任意值,因為真正的長度要在程式碼中依螢幕寬度動態設定。
最後把兩段配置 include 進主版面,並加入 ViewPager:
<!-- activity_main.xml --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.demo.lenovo.myapplication.MainActivity"> <include layout="@layout/top1" /> <include layout="@layout/top2" /> <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
效果如下:

補充一個隱藏標題欄的小技巧:若你的 Activity 繼承自 ActionBarActivity,可在 setContentView() 之前呼叫 requestWindowFeature(Window.FEATURE_NO_TITLE);;若繼承自 AppCompatActivity,則可在 AndroidManifest 的 Application 標籤上設定 android:theme="@style/Theme.AppCompat.NoActionBar",同樣能達到目的。
宣告好成員變數,並在 onCreate 中關掉標題欄:
private FragmentPagerAdapter adapter;
private List<Fragment> mData;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
在 initView() 裡把 Fragment 實例依序放進 List,交給 Adapter 管理,最後掛到 ViewPager 上(三個 Fragment 的版面與程式碼此處省略):
mData = new ArrayList<>();
mData.add(new ChatFragment());
mData.add(new DiscoverFragment());
mData.add(new ContactsFragment());
adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override public Fragment getItem(int position) {
return mData.get(position);
}
@Override public int getCount() {
return mData.size();
}
};
vp_content.setAdapter(adapter);
為 ViewPager 掛上 OnPageChangeListener,在 onPageSelected() 裡先把所有文字歸零成黑色,再把當前分頁上色:
@Override public void onPageSelected(int position) {
Log.e(TAG, "onPageSelected: " + position);
resetTextViewColor();
switch (position) {
case 0:
addBadgeView();
tv_tab_chat.setTextColor(Color.parseColor("#008000"));
break;
case 1:
tv_tab_discover.setTextColor(Color.parseColor("#008000"));
break;
case 2:
tv_tab_contacts.setTextColor(Color.parseColor("#008000"));
break;
}
}
// 首先將每個選項卡文字顏色置為黑色
private void resetTextViewColor() {
tv_tab_contacts.setTextColor(Color.BLACK);
tv_tab_chat.setTextColor(Color.BLACK);
tv_tab_discover.setTextColor(Color.BLACK);
}
寫法是先判斷是否已存在,有的話先移除再重新加入,避免堆疊:
private void addBadgeView() {
if (mBadgeView != null) {
ll_chat.removeView(mBadgeView);
}
mBadgeView = new BadgeView(MainActivity.this);
ll_chat.addView(mBadgeView);
mBadgeView.setBadgeCount(9);
}
這部分的邏輯寫在 onPageScrolled() 裡。先看官方對三個參數的定義:

第一個參數 position 代表目前所在頁,例如從第 0 頁往第 1 頁滑,過程中 position 會一直是 0,直到手放開完成切換才變成 1;positionOffset 是滑動百分比,範圍 0~1;positionOffsetPixels 則是滑動的像素數。
從 0 頁滑到 1 頁時印出的 log:

從 1 頁滑到 2 頁:

從 2 頁回滑到 1 頁:

有了這兩個參數,indicator 的位置就能用 (position + positionOffset) × 螢幕寬度 ÷ 3 算出來。
先把 indicator 的寬度設為螢幕寬度的三分之一:
WindowManager manager = getWindow().getWindowManager(); Display display = manager.getDefaultDisplay(); DisplayMetrics outMetrics = new DisplayMetrics(); display.getMetrics(outMetrics); mScreenOneThird = outMetrics.widthPixels / 3;
這裡 mScreenOneThird 是 int 型別,單位為 px。接著套用到 view 上:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams(); lp.width = mScreenOneThird; iv_tab_line.setLayoutParams(lp);
最後在 onPageScrolled() 中動態更新 marginLeft:
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams();
lp.leftMargin = (int) ((positionOffset * mScreenOneThird) + (mScreenOneThird * position));
iv_tab_line.setLayoutParams(lp);
}
這樣一來,indicator 就會平順地跟著手指移動了。
整體拆下來其實沒有什麼神奇的地方:自訂標題欄、Fragment 選項卡、監聽回調算位置、加上一個開源的 BadgeView,四塊拼起來就是熟悉的微信主頁手感。
自訂控制項實作:可拖拽排序的 ListView
前言最近研究了可拖拽排序的 ListView,效果類似酷狗裡的播放清單排序。但因為要加入自己特有的功能,我折騰了好一段時間。最初接觸的是 GitHub 上的開源專案 Dr
Android 學習筆記(五):Service 全面解析——從分類、生命週期到 Binder 跨進程通信
一、Service 到底是什麼? Service 是 Android 四大組件之一,官方文件對它的定義是: A Service is an application co
用 Wireshark 抓包定位 Android 網路請求的耗時瓶頸
Wireshark 是相當可靠的 TCP 抓包工具,光是四十多 MB 的安裝檔就能想像它的分量。搭配它的表達式篩選器,能在海量封包裡迅速鎖定目標。近期我就靠它推斷出一個網
從零動手打造安卓視頻播放器(一):需求設計與方案取捨
前言 走過「計算器」那個小項目之後,我們對安卓開發的整體流程已經有了初步認識,也積累了一點動手經驗。接下來,不妨把台階再往上抬一層,嘗試做一個真正有點分量的東西——「視頻