总结如何实现Android浮动层,主要是dialog的使用

来源:互联网 发布:2017网络语言 编辑:程序博客网 时间:2024/05/21 10:48

自定义一个类继承自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>
0 0
原创粉丝点击