2017-08-01 55 views
4

由于Android 7.0我们有多个窗口模式。参见实施例在这样的画面:如何在多窗口模式Android 7中获取窗口宽度?

enter image description here

用户可以更改窗口的宽度。

我怎样才能得到真正的窗口宽度?

+0

https://developer.android.com/guide/topics/ui/multi-window.html –

+0

@IntelliJAmiya很抱歉,但我仍然无法找出如何让屏幕宽度 – TOP

回答

0

您可以通过添加布局观察器来注册修饰视图更改。

public class MainActivity extends AppCompatActivity { 

private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     Log.d("Main Activity", "" + getWindow().getDecorView().getHeight()); 
     Log.d("Main Activity", "" + getWindow().getDecorView().getWidth()); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (savedInstanceState == null) { 
     getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(layoutListener); 
    } 
} 

...