开发者

Find the Window containing a View in Android

开发者 https://www.devze.com 2022-12-29 17:31 出处:网络
I can\'t find a way to obtain a reference the Window containing an arbitrary View v. I found getWindowToken, but I can\'t figure out开发者_开发问答 how to use it? Does anyone know how?

I can't find a way to obtain a reference the Window containing an arbitrary View v. I found getWindowToken, but I can't figure out开发者_开发问答 how to use it? Does anyone know how?

Also, does anyone know why it returns an IBinder rather than a Window?


Well... since all views have a reference of the activity that created them (Context), you can use that Context to get a reference of the window. Let me show you this example I wrote some minutes ago:

// main activity
import android.app.Activity;
import android.os.Bundle;
public class GetWindow extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView view = new MyView(this);
        view.changeSomethingInWindow(); // keep an eye on this method
        setContentView(view);
    }
}

Then, inside your view you can do this:

// your view :D
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MyView extends View{
    public MyView(Context context) {
        super(context);
    }

    @Nullable
    private Activity getActivity() {
        if (context == null) return null;
        if (context instanceof Activity) return (Activity) context;
        if (context instanceof ContextWrapper) return getActivity(((ContextWrapper)context).getBaseContext());
        return null;
    }
 
    public void changeSomethingInWindow(){
        // get a reference of the activity
        Activity parent = getActivity();
        if(parent == null) return;
        // using the activity, get Window reference
        Window window = parent.getWindow();
        // using the reference of the window, do whatever you want :D
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

In this case, I change the mode the Window is displayed to Fullscreen. Hope this help you. Tell me if you get in trouble with this.


I know this is an old question but when searched Google still redirects to here, and since the above answer is not completely valid, here is what I've done to change window flags inside the view. (The above code basically does the exact thing anyway, but it assumes all views are attached to an Activity.)

It doesn't get the actual Window instance, but it returns the attributes instead, which allows to update the flags, or anything a window has to offer at this point.

The decor view, or the top view that is attached the window should have an instance of WindowManager.LayoutParams and there, you should be able to set the required flags, because the params have a WindowManager.LayoutParams.flags integer value. Here is how to get the window params.

private WindowManager.LayoutParams tryGetWindowParams()
{
    View view = this;
    if (view.getLayoutParams() instanceof WindowManager.LayoutParams)
        return (WindowManager.LayoutParams) view.getLayoutParams();
    while (view.getParent() instanceof View)
    {
        view = (View) view.getParent();
        if (view.getLayoutParams() instanceof WindowManager.LayoutParams)
            return (WindowManager.LayoutParams) view.getLayoutParams();
    }
    return null;
}

After that, you should be able to just do this (the example will be the FLAG_NOT_TOUCHABLE flag, but you can use any other window flag as required.)

private boolean setWindowNotTouchable()
{
    WindowManager.LayoutParams windowParams = tryGetWindowParams();
    if (windowParams != null)
    {
        if ((windowParams.flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) == 0)
        {
            windowParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            requestLayout();
        }
        return true;
    }
    return false;
}

private boolean setWindowTouchable()
{
    WindowManager.LayoutParams windowParams = tryGetWindowParams();
    if (windowParams != null)
    {
        if ((windowParams.flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE) != 0)
        {
            windowParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            requestLayout();
        }
        return true;
    }
    return false;
}

Don't forget to call View.requestLayout() as the window params need to get updated.

0

精彩评论

暂无评论...
验证码 换一张
取 消