編輯:關於 Android 程式設計
Dialog 是對話框的基底類別,可實作以下子類:AlertDialog、DatePickerDialog、TimePickerDialog。這些類別為你定義了樣式與結構,而你也可以使用 DialogFragment 作為對話框的內容。透過 DialogFragment,你能更自由地控制對話框,而不必沿用 Dialog 物件的一些既有方法。
使用時要特別注意 DialogFragment 的生命週期,尤其是使用者按返回鍵或旋轉螢幕時。此外,DialogFragment 也允許你把它的 UI 用於一般畫面上。
若使用 Support Library,請記得引用 android.support.v4.app.DialogFragment 而非 android.app.DialogFragment。
public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); } }
當你想顯示對話框時,只需呼叫 show()。
在 DialogFragment 中設定一個介面,透過這個介面就能把事件傳遞到主 Activity 中。
public class NoticeDialogFragment extends DialogFragment { /* The activity that creates an instance of this dialog fragment must * implement this interface in order to receive event callbacks. * Each method passes the DialogFragment in case the host needs to query it. */ public interface NoticeDialogListener { public void onDialogPositiveClick(DialogFragment dialog); public void onDialogNegativeClick(DialogFragment dialog); } // Use this instance of the interface to deliver action events NoticeDialogListener mListener; // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (NoticeDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } ... }
public class CustomDialogFragment extends DialogFragment { /** The system calls this to get the DialogFragment's layout, regardless of whether it's being displayed as a dialog or an embedded fragment. */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout to use as dialog or embedded fragment return inflater.inflate(R.layout.purchase_items, container, false); } /** The system calls this only when creating the layout in a dialog. */ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // The only reason you might override this method when using onCreateView() is // to modify any dialog characteristics. For example, the dialog includes a // title by default, but your custom layout might not need it. So here you can // remove the dialog title, but you must call the superclass to get the Dialog. Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); return dialog; } }
public void showDialog() { FragmentManager fragmentManager = getSupportFragmentManager(); CustomDialogFragment newFragment = new CustomDialogFragment(); if (mIsLargeLayout) { // The device is using a large layout, so show the fragment as a dialog newFragment.show(fragmentManager, "dialog"); } else { // The device is smaller, so show the fragment fullscreen FragmentTransaction transaction = fragmentManager.beginTransaction(); // For a little polish, specify a transition animation transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); // To make it fullscreen, use the 'content' root view as the container // for the fragment, which is always the root view for the activity transaction.add(android.R.id.content, newFragment) .addToBackStack(null).commit(); } }
其中 mIsLargeLayout 會依當前設備判斷是否使用 App 的大尺寸 UI 佈局。取值方式如下:
res/values/bools.xml
false
res/values-large/bools.xml
true
boolean mIsLargeLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIsLargeLayout = getResources().getBoolean(R.bool.large_layout); }
如果你的應用只針對小螢幕手機設計,可以將 Activity 主題設為 Theme.Holo.DialogWhenLarge,這樣在大螢幕上就會以對話框的形式呈現。
【Android 仿微信通訊錄 下】自訂 View 為 RecyclerView 打造右側索引導航欄 IndexBar
一、概述 我們用 ItemDecoration 為 RecyclerView 打造了帶懸浮頭部的分組列表。其實 Android 版微信的通訊錄介面,它的分組 title
Android 自訂下拉刷新與上拉加載完整實作
本文將分享如何使用 SwipeRefreshLayout 搭配 RecyclerView 的多 ViewType 機制,實作下拉刷新與上拉加載,供需要的朋友參考。 先看效
Android Service 詳解(二):以 Binder 綁定服務,讓 Activity 直接指揮後台
上一篇《Android 中 Service 服務詳解(一)》談的是透過 Context 的 startService() 與 stopService() 來啟動與停止服務
Android 實現圖片倒影效果:Matrix 翻轉搭配漸層遮罩
要做出水面上那種倒影感,關鍵是兩件事:把原圖垂直翻轉貼在下半部,再從上到下疊一層由不透明漸/to 透明的漸層,讓倒影越往下越淡。 效果預覽 核心程式碼 全透明