我正在尝试创建透明叠加层,应该使用下面的代码在其他应用程序窗口之上工作。
问题是:
1)它不能在其他应用程序窗口的顶部工作
2)背景颜色不透明。
java代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//-----
final WindowManager.LayoutParams window_params=getWindow().getAttributes();
// window_params.screenBrightness=1f;
window_params.format = PixelFormat.TRANSPARENT;
window_params.type=2006;
getWindow().setAttributes(window_params);
//------
view = new MyView(this); //MyView view = new MyView(this);
view.setBackgroundColor(Color.TRANSPARENT);
view.changeSomethingInWindow(); // keep an eye on this method
setContentView(R.layout.main); //setContentView(view); //setContentView(view ,new LayoutParams(200,400));
}
public class MyView extends View{
public MyView(Context context) {
super(context);
}
public void changeSomethingInWindow(){
// get a reference of the activity
Activity parent = (Activity)getContext();
Window window = parent.getWindow();
window.getWindowManager();
window.setLayout(300, 400); //window.setLayout(getHeight()/2, getWidth()/2);
window.setFormat(PixelFormat.TRANSPARENT);
//window = new Window(this, R.style.Theme_Transparent);
// window.setBackgroundDrawableResource(getWindowVisibility());
window.setFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
window.getWindowStyle();
window.getDecorView();
}
}
style.xml:
<?xml version="1.0" encoding="utf-8"?> <resources>
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
manifest.xml:android:theme="@style/Theme.Transparent"
我在manifest.xml中使用过它
试着帮助我,弄清楚这个问题的解决方案。 感谢。
答案 0 :(得分:1)
尝试类似的东西:
private static final int FLAGS = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
WindowManager.LayoutParams params = new WindowManager.LayoutParams(0, FLAGS, PixelFormat.TRANSLUCENT);
getWindowManager().addView (overlayView, params);
如果您想要接收触摸,请删除FLAG_NOT_TOUCHABLE。 注意“addView”参数。