自定义Dialog

来源:互联网 发布:php接收post json 编辑:程序博客网 时间:2024/06/05 19:01

1.定义Dialog样式

styles.xml

 <style name="add_dialog" parent="@android:style/Theme.Dialog">        <!-- 边框 -->        <item name="android:windowFrame">@null</item>        <!-- 是否浮现在activity之上 -->        <item name="android:windowIsFloating">true</item>        <!-- 半透明 -->        <item name="android:windowIsTranslucent">false</item>        <!-- 无标题 -->        <item name="android:windowNoTitle">true</item>        <!-- 自己想要的背景 -->        <item name="android:windowBackground">@drawable/new_message_background</item>        <!-- 模糊 -->        <item name="android:backgroundDimEnabled">false</item>    </style>

2.自定义layout(根据自己的需要自定义)
3.自定义View

public class CustomDialog extends Dialog {    private View view;    public CustomDialog(Context context, int themeResId) {        super(context, themeResId);        //此处加载的布局就是上面自定义的layout        LayoutInflater inflater = LayoutInflater.from(context);        view = inflater.inflate(R.layout.new_message_dialog, null);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(view);    }    //获取自定义layout中的控件    @Override    public View findViewById(int id) {        return super.findViewById(id);    }    //获取自定义布局view    public View getView() {        return view;    }}

4.实例化

Dialog dialog = new CustomDialog(Context对象,R.style.add_dialog);//        Window dialogWindow = dialog.getWindow();//        WindowManager.LayoutParams lp = dialogWindow.getAttributes();//        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);//        lp.x = 100; // 新位置X坐标//        lp.y = 100; // 新位置Y坐标//        lp.width = 300; // 宽度//        lp.height = 100; // 高度//        dialogWindow.setAttributes(lp);        dialog.show();
0 0