Cas*_*ash 27 user-interface android
我找不到一种方法来获取Window包含任意Viewv 的引用.我找到了getWindowToken,但我无法弄清楚如何使用它?有谁知道怎么样?
此外,有谁知道为什么它返回IBinder而不是Window?
Cri*_*ian 18
好吧......因为所有视图都有创建它们的活动的引用(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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的视图中,您可以执行以下操作:
// 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我将Window显示的模式更改为Fullscreen.希望这对你有所帮助.如果你遇到麻烦,请告诉我.
| 归档时间: |
|
| 查看次数: |
26694 次 |
| 最近记录: |