没有状态栏,动作条和标签的屏幕高度
问题描述:
我有一个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;
但是,列表的行会稍微太大了,我想它的状态栏造成它。我如何将它的高度加到我的计算中?
答
基于@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
请尝试使用此解决方案,并告诉我们,如果它的工作。 ;)
答
最简单的方法是这样的:
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>
你能完整地发布你的课程的XML文件吗? – apmartin1991