Android如何找到可用的显示区域

Android如何找到可用的显示区域,android,Android,我试图在我的应用程序中找到可用的显示区域,这样我就可以通过编程来调整大小。到目前为止,我有以下几点 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu

我试图在我的应用程序中找到可用的显示区域,这样我就可以通过编程来调整大小。到目前为止,我有以下几点

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        Point size = new Point();
        display.getSize(size);


        final TypedArray styledAttributes = getBaseContext().getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
        int actionBarHeight = (int) styledAttributes.getDimension(0, 0);
        styledAttributes.recycle();

        int usableHeight = size.y - actionBarHeight;
这在大多数情况下都有效,但我有一个平板电脑,上面有一个菜单栏(32像素高),包含时间、网络状态、电池电量等(不知道是否有真实名称),底部有一个栏(48像素高),有后退按钮、应用程序键和屏幕截图按钮

我的设备是800x600,所以当我显示时;我得到了552=600——底部的48像素条。问题是,它不会减去顶部的条

那么,如何访问顶部栏(如果存在),以便找到真正的应用程序区域

我还有一款平板电脑,它的时间、电池电量和wifi状态与“后退”按钮、应用程序按钮、屏幕截图等位于同一条上,所以它没有问题


感谢

为您需要测量的应用程序提供可用的屏幕区域 您自己应用程序的父视图:

   final View view = getWindow().getDecorView().getRootView();

   int scrLoc[] = new int[2];
   top.getLocationOnScreen(scrLoc);
   int BottomLoc[] = new int[2];
   bottom.getLocationOnScreen(BottomLoc);
   boolean portrait = getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE;
   if (portrait) {
      availableWidth = top.getWidth();
      availableHeight = BottomLoc[1] - scrLoc[1];
   } else {
      availableWidth = top.getWidth();
      availableHeight = BottomLoc[1] - scrLoc[1];
   }
而且,如果您需要在onResume(比如onCreate)之前进行测量,您将 需要在回调中包装此代码

最后,如果你的应用程序对屏幕尺寸敏感,你可能还想改变它的尺寸 切换屏幕方向时的内部状态。这样做

在舱单中:

<activity android:name=".MainActivity"
    android:label="@string/app_name"
    android:configChanges="orientation">

哇,比我想象的要多得多。我需要在屏幕渲染之前执行此操作,OnCreateOptions菜单是我的最佳位置。我怎么把它放进去?谢谢。嗨,在这个例子中,如何初始化顶部和底部?谢谢
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);      
    // your change orientation logic here
}