在 Android 中查找包含 View 的 Window
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2785558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the Window containing a View in Android
提问by Casebash
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?
我找不到获取Window
包含任意View
v的引用的方法。我找到了getWindowToken,但我不知道如何使用它?有谁知道怎么做?
Also, does anyone know why it returns an IBinder
rather than a Window
?
另外,有谁知道为什么它返回一个IBinder
而不是一个Window
?
采纳答案by Cristian
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:
嗯...因为所有视图都有创建它们的活动的引用 ( Context
),您可以使用它Context
来获取窗口的引用。让我给你看看我几分钟前写的这个例子:
// 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);
}
public void changeSomethingInWindow(){
// get a reference of the activity
Activity parent = (Activity)getContext();
// 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.
在这种情况下,我将窗口的显示模式更改为全屏。希望这对你有帮助。如果你遇到这个问题,请告诉我。
回答by Furkan Yurdakul
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.
它没有获得实际的Window
实例,而是返回属性,这允许更新标志,或此时窗口必须提供的任何内容。
The decor view, or the top view that is attached the window shouldhave 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.
装饰视图或附加到窗口的顶视图应该有一个实例,WindowManager.LayoutParams
并且在那里,您应该能够设置所需的标志,因为参数具有WindowManager.LayoutParams.flags
整数值。这是获取窗口参数的方法。
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.)
之后,您应该能够做到这一点(示例将是 FLAG_NOT_TOUCHABLE 标志,但您可以根据需要使用任何其他窗口标志。)
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.
不要忘记调用,View.requestLayout()
因为窗口参数需要更新。