将自定义布局设置为屏幕高度

时间:2015-12-15 10:57:48

标签: android android-layout android-screen-support

我必须在屏幕高度上添加一个布局,删除操作栏高度。我正在使用下面的代码 -

View popupView = getLayoutInflater().inflate(
                R.layout.left_interaction_layout, null);
        double screenSize = 0;
    try {
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        int dens = dm.densityDpi;
        double wi = (double) width / (double) dens;
        double hi = (double) height / (double) dens;
        double x = Math.pow(wi, 2);
        double y = Math.pow(hi, 2);
        screenSize = Math.sqrt(x + y);
    } catch (Exception e) {
        // TODO: handle exception
    }
        double screenByTwoSize = screenSize / 3;

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // int width = metrics.widthPixels;
        // int height = metrics.heightPixels;
        float mXDpi = metrics.xdpi;
        float mYDpi = metrics.ydpi;
        int screenWidth = (int) Math.round(screenByTwoSize * mXDpi);
        int screenHeight = (int) Math.round(screenByTwoSize * mYDpi);

        popup = new PopupWindow(popupView, screenWidth, 200, true);
        popup.setBackgroundDrawable(new BitmapDrawable());
        // Get ListView object from xml
        final ListView listView = (ListView) popupView
                .findViewById(R.id.list);
        try {
            popup = new PopupWindow(popupView, 120, screenHeight * 2 + 250,
                    true);
            popup.setBackgroundDrawable(new BitmapDrawable());
        } catch (Exception e) {
            // TODO: handle exception
        }

        popup.setOutsideTouchable(true);
        // popup.showAsDropDown(tvcountry);
        Rect location = LoginActivity.locateView(v);
        popup.showAtLocation(v, Gravity.TOP | Gravity.LEFT, 0,
                location.bottom);
        popup.update();
        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {

            }
        });

我有硬编码的高度,对于不同的屏幕尺寸可能会有所不同。我怎样才能动态实现这一目标。我必须通过移除操作栏高度使此布局达到屏幕高度,以便它像导航抽屉一样工作

先谢谢

1 个答案:

答案 0 :(得分:0)

您可以按如下方式计算ActionBar高度

  TypedValue tv = new TypedValue();
    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
    }

并删除带有实际高度的操作栏高度