總結如何實現Android浮動層,主要是dialog的使用。
自定義一個類繼承自Dialog類,然后在構造方法中,定義這個dialog的布局和一些初始化信息。
public class MenuDialog extends Dialog { public MenuDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); // TODO Auto-generated constructor stub } public MenuDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub } public MenuDialog(Context context) { //dialog的視圖風格 super(context, R.style.Theme_Transparent); //設置布局文件 setContentView(R.layout.menu_dialog); //setTitle("Custom Dialog"); //單擊dialog之外的地方,可以dismiss掉dialog。 setCanceledOnTouchOutside(true); // 設置window屬性 // LayoutParams a = getWindow().getAttributes(); // getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // a.gravity = Gravity.TOP; // a.dimAmount = 1.0f; // 添加背景遮蓋 // getWindow().setAttributes(a); //在下面這種情況下,后臺的activity不會被遮蓋,也就是只會遮蓋此dialog大小的部分 LayoutParams a = getWindow().getAttributes(); a.gravity = Gravity.TOP; a.dimAmount = 0.0f; // 去背景遮蓋 getWindow().setAttributes(a); //為你的對話框初始化數據 initMenu(); } }
然后再需要此dialog的地方,實例化這個dialog就行了。
另附此對話框的主題:
<style name="Theme.Transparent" parent="android:Theme"> <item name="android:windowBackground">@drawable/dialog_box_2</item>//此對話框的背景 <item name="android:windowIsTranslucent">true</item>//對話框是否透明 <item name="android:windowContentOverlay">@null</item>//對話框是否有遮蓋 <item name="android:windowNoTitle">true</item>//對話框無標題 <item name="android:windowIsFloating">true</item>//對話框是否浮動 <item name="android:backgroundDimEnabled">false</item> </style>
文章出處:http://blog.csdn.net/ooo4561213/article/details/6655748
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!