我有一个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 :(得分:10)
根据@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.
答案 1 :(得分:3)
您可以使用此代码:
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://stackoverflow.com/a/3410200/1763138
请尝试使用此解决方案并告诉我们它是否有效。 ;)
答案 2 :(得分:1)
最简单的方法是:
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>
答案 3 :(得分:0)
这里有一些可以帮助您的扩展
fun Activity.heightScreen(): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
windowManager.currentWindowMetrics.bounds.height()
} else {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics.heightPixels
}
}
/***
* Gets the real ]Height of the display without subtracting any window decor or
* applying any compatibility scale factors.
* <p>
* The size is adjusted based on the current rotation of the display.
* @param view - your view
*/
fun Fragment.realHeightScreen(view: View = requireView()): Int {
val point = Point()
view.display.getRealSize(point)
return point.y
}
/***
*Gets the real Width of the display without subtracting any window decor or
* applying any compatibility scale factors.
* <p>
* The size is adjusted based on the current rotation of the display.
* @param view - your view
*/
fun Fragment.realWidthScreen(view: View = requireView()): Int {
val point = Point()
view.display.getRealSize(point)
return point.x
}
fun getSoftNavigationBarSize(resources: Resources = Resources.getSystem()): Int {
var result = 0
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
return result
}