編輯:關於 Android 程式設計
研究 Contacts 源碼時發現它用 TabHost 組織了通話界面,於是自己照著寫了一個 TabHostTest,順便把「Tab 裡再套 Tab」的嵌套用法一起整理出來。
注意 FrameLayout 這裡用 layout_height="0dip" 搭配 layout_weight="1",讓內容區自動吃掉剩餘空間,標籤欄則按內容高度排布。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
這裡刻意把 TabWidget 放到 FrameLayout 之後,讓二級標籤欄呈現在頁面底部——這也是 Contacts 的做法。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
每個 Tab 的內容都用 Intent 指向 Inner.class(收藏頁除外),並依 Intent 內容決定啟動時落在哪個 Tab。
package com.android.test;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts.Intents.UI;
import android.view.Window;
import android.widget.TabHost;
public class Main extends TabActivity implements TabHost.OnTabChangeListener {
private static final int TAB_INDEX_DIALER = 0;
private static final int TAB_INDEX_CALL_LOG = 1;
private static final int TAB_INDEX_CONTACTS = 2;
private static final int TAB_INDEX_FAVORITES = 3;
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
//Setup the tabs
setupDialerTab();
setupCallLogTab();
setupContactsTab();
setupFavoritesTab();
setCurrentTab(intent);
}
public void onTabChanged(String tabId) {
Activity activity = getLocalActivityManager().getActivity(tabId);
if (activity != null) {
activity.onWindowFocusChanged(true);
}
}
private void setupCallLogTab() {
//Force the class since overriding tab entries doesn't work
Intent intent = new Intent("com.android.phone.action.RECENT_CALLS");
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("call_log")
.setIndicator("通話記錄", getResources().getDrawable(R.drawable.ic_tab_unselected_recent))
.setContent(intent));
}
private void setupDialerTab() {
Intent intent = new Intent("com.android.phone.action.TOUCH_DIALER");
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("dialer")
.setIndicator("撥號", getResources().getDrawable(R.drawable.ic_tab_unselected_dialer))
.setContent(intent));
}
private void setupContactsTab() {
Intent intent = new Intent(UI.LIST_DEFAULT);
intent.setClass(this, Main.class);
mTabHost.addTab(mTabHost.newTabSpec("contacts")
.setIndicator("通訊錄", getResources().getDrawable(R.drawable.ic_tab_unselected_contacts))
.setContent(intent));
}
private void setupFavoritesTab() {
Intent intent = new Intent(UI.LIST_STREQUENT_ACTION);
intent.setClass(this, Inner.class);
mTabHost.addTab(mTabHost.newTabSpec("favorites")
.setIndicator("收藏", getResources().getDrawable(R.drawable.ic_tab_unselected_starred))
.setContent(intent));
}
/**
* 依 intent 的請求類型決定要選中哪個 tab
*
* @param intent 包含 tab 選擇資訊的 Intent
*/
private void setCurrentTab(Intent intent) {
// Dismiss menu provided by any children activities
Activity activity = getLocalActivityManager()
.getActivity(mTabHost.getCurrentTabTag());
if (activity != null) {
activity.closeOptionsMenu();
}
// Tell the children activities that they should ignore any possible saved
// state and instead reload their state from the parent's intent
intent.putExtra("", true);
// Choose the tab based on the inbound intent
String componentName = intent.getComponent().getClassName();
if (getClass().getName().equals(componentName)) {
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// launched from history (long-press home) --> nothing to change
} else {
// The dialer was explicitly requested
mTabHost.setCurrentTab(TAB_INDEX_DIALER);
}
}
}
}
這裡示範了幾個實用技巧:關掉標籤底部分割線(setStripEnabled(false))、遍歷 TabWidget 的子 View 調整文字顏色、內距與高度、以及替換背景。
package com.android.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class Inner extends TabActivity implements TabHost.OnTabChangeListener {
private static final int TAB_INDEX_ALL = 0;
private static final int TAB_INDEX_MISSED = 1;
private static final int TAB_INDEX_OUTGOING = 2;
private static final int TAB_INDEX_RECEIVED = 3;
private TabHost mTabHost;
private TabWidget mTabWidget;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.inner);
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
setupTabs();
mTabWidget = mTabHost.getTabWidget();
//去掉標籤下方的分隔線
mTabWidget.setStripEnabled(false);
for (int i = 0; i < mTabWidget.getChildCount(); i++) {
TextView tv = (TextView) mTabWidget.getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
tv.setPadding(0, 0, 0, (int) tv.getTextSize());
tv.setText("Tab" + i);
mTabWidget.getChildAt(i).getLayoutParams().height =
(int) (3 * tv.getTextSize());
mTabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
}
public void onTabChanged(String tabId) { }
private void setupTabs() {
mTabHost.addTab(mTabHost.newTabSpec("all")
.setIndicator(getString(R.string.inner))
.setContent(new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Missed")
.setIndicator(getString(R.string.inner))
.setContent(new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Outgoing")
.setIndicator(getString(R.string.inner))
.setContent(new Intent(this, Other.class)));
mTabHost.addTab(mTabHost.newTabSpec("Received")
.setIndicator(getString(R.string.inner))
.setContent(new Intent(this, Other.class)));
}
}

整體看下來,TabHost 的關鍵約束只有一條:那三個 id 不能改。剩下的自由度其實相當高,透過調整 TabWidget 的位置與遍歷修改子 View 屬性,就能做出與系統默認風格完全不同的效果。
非同步載圖第二彈:修掉崩潰、補上進度,並換用 NumberProgressBar
上一節我們用 AsyncTask 完成了非同步下載圖片,但留下了三個問題:無網路時點擊會崩潰、進度條沒有真的更新、以及只能用系統內建的 ProgressBar。這次逐一解
Android 餐飲管理 App 實作筆記(二):用 SlidingMenu 搭出側滑菜單
這個專案要做的,是一套跑在 Android 平板或手機上的小型餐飲管理系統:錄入菜品、點餐開單、數據統計,還要能連小米或佳博的藍牙小票機。功能不少,但整個 App 幾乎都
【Android】仿今日頭條風格的下拉刷新實作筆記
點一下按鈕會自動觸發下拉刷新,也可以用手勢手動刷新;每完成一次刷新,列表就多出一筆資料。整個頁面由四個分頁(Tab)切換組成。 這個效果其實來自導師交代的專案:替本科同
Android 5.0 新增能力:用 Palette 為界面自動配色
Palette 既好用又好玩。它的作用是從圖像中提取突出的顏色,我們可以把提取到的色值賦給 Toolbar、標題、狀態欄等,讓整個界面的色調保持統一,視覺效果非常出色。