總結如何實現浮動層,主要是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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章