我想制作一个红色的遮罩层窗口。您可以在下面看到我的代码。当我点击按钮时,它没有做任何事情。问题出在哪里?我该如何修复?请提出建议。 谢谢!
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, OverlayLayer.class);
startService(intent);
}
});
}
}
public class OverlayLayer extends Service {
public IBinder onBind(Intent ıntent) {
return null;
}
private LinearLayout mLayout;
private WindowManager mManager;
@Override
public void onCreate() {
super.onCreate();
mManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mLayout = new LinearLayout(this);
LinearLayout.LayoutParams mParams=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
mLayout.setBackgroundColor(Color.rgb(255,255,0));
mLayout.setLayoutParams(mParams);
WindowManager.LayoutParams parameters=new WindowManager.LayoutParams(400,150,WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);
parameters.x=0;
parameters.y=0;
parameters.gravity = Gravity.CENTER;
mManager.addView(mLayout,parameters);
}
}