没有状态栏,动作条和标签的屏幕高度

问题描述:

我有一个ListView,我希望每行填充可用屏幕的三分之一。我可以看到状态栏,然后是一个带有下方的slidingTabs的actionBar。我做的电流计算如下:没有状态栏,动作条和标签的屏幕高度

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()); 
    } 

和设置的意见高度是这样的:

holder.imageView.getLayoutParams().height = (height - actionBarHeight*2)/3; 

但是,列表的行会稍微太大了,我想它的状态栏造成它。我如何将它的高度加到我的计算中?

+0

你能完整地发布你的课程的XML文件吗? – apmartin1991

基于@rasmeta的答案我做了这段代码,它的确有用。我的行现在只是可用屏幕的三分之一。这里是如何让状态栏的高度:

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. 

您可以使用此代码:

public int getStatusBarHeight() { 
     int result = 0; 
     int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
     if (resourceId > 0) { 
      result = getResources().getDimensionPixelSize(resourceId); 
     } 
     return result; 
} 

如下所述:https://*.com/a/3410200/1763138

请尝试使用此解决方案,并告诉我们,如果它的工作。 ;)

+1

尽管这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – mmackh

+0

谢谢你的建设性意见 - 你是对的。我已经为未来的访问者编辑了答案。 – rasmeta

最简单的方法是这样的:

int height = findViewById(R.id.rootView).getHeight(); 

rootView是主根布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootView" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
     ... 
</LinearLayout>