屏幕高度,不包括状态栏,actionBar和选项卡 [英] Height of screen without status bar, actionBar and tabs
问题描述
我有一个ListView,我希望每一行都填充可用屏幕的三分之一.我有可见的状态栏,然后是一个带有滑动标签的actionBar.我正在像这样进行当前计算:
I have a ListView that I want each row to fill a third of the available screen. I have the status bar visible, and then an actionBar with slidingTabs beneath. I'm doing the current calculation like this:
height = context.getResources().getDisplayMetrics().heightPixels;
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,context.getResources().getDisplayMetrics());
Log.d("actionBarHeigth", actionBarHeight.toString());
}
并按如下所示设置视图高度:
And setting the views height like this:
holder.imageView.getLayoutParams().height = (height - actionBarHeight*2) / 3;
但是列表的行有点太大,我想这是导致它的状态栏.如何将其高度添加到计算中?
But the rows of the list are slightly too big, and i guess it's the status bar causing it. How can I add the height of it to my calculations?
推荐答案
基于@rasmeta的回答,我编写了此代码,并且成功了.现在,我的行正好是可用屏幕的三分之一.这是获取状态栏高度的方法:
Based on @rasmeta 's answer I made this code and it does the trick. My rows are now exactly a third of the available screen. Here is how to get the height of the status bar:
int resource = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resource > 0) {
statusBarHeight = context.getResources().getDimensionPixelSize(resource);
}
每行高度的计算如下:
holder.imageView.getLayoutParams().height = (screenHeight - statusBarHeight - actionBarHeight*2) / 3;
// actionBarHeight * 2 because the tabs have the same height as the actionBar.
这篇关于屏幕高度,不包括状态栏,actionBar和选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!