Service中创建Dialog
public void createWindow(){
Dialog dialog = new Dialog(getApplicationContext());
Window window = dialog.getWindow();
//service中添加window使用该类型,
//并需要声明权限:
//<uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY"/>
//<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
window.setGravity(Gravity.LEFT|Gravity.TOP);
//设置Dialog背景
window.setBackgroundDrawableResource(R.color.white);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.x = 0;
layoutParams.y = 500;
//layoutParams.width = 450;
//layoutParams.height = 300;
window.setAttributes(layoutParams);
//去除title
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setContentView(R.layout.activity_main);
dialog.show();
}
将布局变为圆角
1.在布局中添加自己的background
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/layout_border"
>
<TextView
android:text="这是一个TextView"
android:layout_width="450px"
android:layout_height="300px"/>
</LinearLayout>
2.创建background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<corners android:radius="20px" />
</shape>
设置Background 为白色,圆角为20px