1

我正在尝试创建一个透明覆盖,应该在其他应用程序窗口的顶部也使用下面的代码。问题是:
1)它在其他应用程序窗口的顶部不起作用。
2)背景颜色不透明。

爪哇代码:

@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();


    }
}   

样式.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 中使用过这个

尝试帮助我,找出这个问题的解决方案。谢谢。

4

1 回答 1

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”参数。

于 2012-08-07T05:23:04.007 回答