查找包含在Android的视图窗口视图、窗口、Android

2023-09-04 12:26:44 作者:夜灵雪

我无法找到一个方法来获得一个参考窗口包含任意查看诉我发现getWindowToken,但我无法弄清楚如何使用它?有谁知道?

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?

此外,没有人知道为什么它返回一个的IBinder ,而不是窗口

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);
    }

    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.