編輯:關於 Android 程式設計
我習慣從實際工作中取材。最近需要在相機裡加入聲控拍照(語音拍照)功能,在設定頁面中要實現如下效果:
這個「語音拍照」菜單的需求如下:
(1) 點擊「語音拍照」菜單時彈出一個 Dialog,點擊其中的「拍照」/「茄子」會自動播放聲音;
(2) Dialog 出現時,只要點擊 Dialog 以外的區域,它就會自動消失;
(3) 點擊菜單最右側的綠色 switch 按鈕時,可以開啟/關閉語音拍照功能。
上述效果其實是一個 PreferenceActivity,因此新增的「語音拍照」菜單就加在這個 PreferenceActivity(CameraSettingActivity)裡。
Activity 中有一個方法 public Dialog onCreateDialog(int dialogId),用它就能創建屬於自己的 Dialog,再呼叫 showDialog(int id) 顯示即可。Activity 會依不同的 dialogId 創建並顯示不同的 Dialog,而 dialogId 由我們自行定義,例如:
private static final int DIALOG_ID_VOICE_COMMAND_SHOW_TONES = 111;
接著覆寫父 Activity 的 onCreateDialog 方法:
@Override
public Dialog onCreateDialog(int dialogId) {
if (dialogId == DIALOG_ID_VOICE_COMMAND_SHOW_TONES) {
Dialog dialog = new Dialog(this, R.style.transparent_dialog_them);
dialog.setContentView(R.layout.setting_switch_sublist_layout);
VoiceManager voice_manager = ((CameraApp) getApplication()).getVoiceManager();
SettingSwitchSublistLayout mVoiceSettingLayout =
dialog.findViewById(R.id.SettingSwitchSublistLayout_ID);
mVoiceSettingLayout.initialize(voice_manager.getVoiceEntryValues());
mVoiceSettingLayout.setSettingChangedListener(this);
dialog.getWindow().setCloseOnTouchOutside(true);
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
return super.onCreateDialog(dialogId);
}
關鍵在於 new Dialog(this, R.style.transparent_dialog_them) 中的主題參數。這裡需要把背景設為透明、無標題:
<style name="transparent_dialog_them" parent="@style/Theme.HWDroid.Ali.Dialog.NoActionBar">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>
這裡有一個值得玩味的細節:創建一個 Dialog,為什麼會跟 window、action、title 扯上關係?其實,查看 Dialog 的實作類別就會發現,創建一個 Dialog 就等於創建一個 window,而 window 自然就有 action、title 等屬性。同樣地,創建 Activity 本質上也是創建 window——一個介面的呈現都起源於一個 window,它不僅管理介面顯示,所有設備輸入事件也都從這裡出發。
實際上我採用的仍是平常使用的 SwitchPreference,只是自定義了它的布局。SwitchPreference 的 setLayoutResource 方法就能配置自定義布局:
SwitchPreference voicePref = (SwitchPreference)
findPreference(getString(R.string.camera_setting_item_pref_voice_key));
voicePref.setLayoutResource(R.layout.xunhu_voice_preference);
這個布局跟預設的 SwitchPreference 幾乎沒差別,只是在父 view 上加了一個屬性 android:onClick="VoiceCommandClickListener"。而 VoiceCommandClickListener 的定義很簡單:
public void VoiceCommandClickListener(View v) {
showDialog(DIALOG_ID_VOICE_COMMAND_SHOW_TONES);
}
就是呼叫 showDialog 顯示我們自己的 Dialog。
寫到這裡,浮現兩個疑問:(1) 點擊 Dialog 以外區域自動消失,是否有更簡單的做法?(2) 能否用 DialogFragment 來替換這裡的 Dialog?
先看第一個。透過 HierarchyViewer 查看會發現,這個 Dialog 其實佔據了整個螢幕,只是我們把它的背景設成了全透明。也就是說,你根本點不到「Dialog 以外的區域」,所以 setCloseOnTouchOutside 才看似失效。若把主題改成繼承自 Theme.Dialog(如上文 transparent_dialog_them 的父改為 Theme.HWDroid.Ali.Dialog.NoActionBar),Dialog 就不再佔滿全屏,點擊外部區域即可正常關閉——這也印證了「Dialog 與 Activity 的顯示都起源於 window」的推斷。
再看第二個。查看 Activity.java 中與 Dialog 相關的源碼,會看到這樣的註記:
* @deprecated Use the new {@link DialogFragment} class with
* {@link FragmentManager} instead; this is also available on older
* platforms through the Android compatibility package.
也就是說,官方已不再推薦直接使用 Dialog,而是建議改用 DialogFragment。而 DialogFragment 本質上就是一個封裝了 Dialog 的 Fragment——會 Fragment,就會 DialogFragment。下面直接看採用 DialogFragment 的實現:
public void VoiceCommandClickListener(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
VoiceCommandDialogFragment prev =
(VoiceCommandDialogFragment) getFragmentManager().findFragmentByTag("voice_dialog");
if (prev != null) {
ft.show(prev);
} else {
prev = VoiceCommandDialogFragment.newInstance();
prev.show(ft, "voice_dialog");
}
}
private static final class VoiceCommandDialogFragment extends DialogFragment {
private static class SingletonHolder {
private static VoiceCommandDialogFragment f = new VoiceCommandDialogFragment();
}
public static VoiceCommandDialogFragment newInstance() {
return SingletonHolder.f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int style = DialogFragment.STYLE_NO_TITLE, theme = R.style.transparent_dialog_them;
setStyle(style, theme);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.setting_switch_sublist_layout, container, false);
VoiceManager voice_manager = ((CameraApp) getActivity().getApplication()).getVoiceManager();
SettingSwitchSublistLayout mVoiceSettingLayout =
v.findViewById(R.id.SettingSwitchSublistLayout_ID);
mVoiceSettingLayout.initialize(voice_manager.getVoiceEntryValues());
mVoiceSettingLayout.setSettingChangedListener((CameraSettingActivity) getActivity());
return v;
}
}
這裡有三點需要注意:(1) 用單例模式(內部類 SingletonHolder)避免重複創建 DialogFragment;(2) 顯示前先檢測 FragmentManager 中是否已存在該 DialogFragment;(3) 主題務必設為 dialog 主題。做到這三點,一個更規範、更好維護的自定義 Dialog 就完成了。
Android 使用 TextView 實現跑馬燈效果
最近忙於項目,很久沒有總結博客和提交 GitHub 了。接下來我打算把項目中一些比較有用的內容整理出來發表到博客上,也計劃總結一些關於設計模式與源碼分析的文章。今天先來講
Android 底部彈出 iOS 7 風格動作選單:第三方開源庫 IOS_Dialog_Library 實作教學
先看一下成果,有興趣的朋友可以下載源碼動手試試。 關於 IOS_Dialog_Library 完成上述效果所使用的是開源庫 IOS_Dialog_Librar
Android 開發之自定義 View 專題(二):自定義餅圖
在圖表類需求裡,最常見的莫過於折線圖、柱形圖與餅圖。上周已分享過柱形圖的實作,而項目裡其實還用到餅圖,只是沒機會用折線圖。其實學會其中一個,再寫其他的大致也就知道該怎麼下
Android 系統移植實戰:客製化開機後的系統預設設定
本文記錄一次針對特定客戶機型客製化 Android 系統預設值的完整過程,包含開發者選項、USB 除錯與未知來源的預設開啟。 一、讓「開發者選項」預設顯示 相關源碼位於