【问题标题】:Height of screen without status bar, actionBar and tabs没有状态栏、动作栏和选项卡的屏幕高度
【发布时间】:2020-12-22 05:51:21
【问题描述】:

我有一个 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;

但是列表的行有点太大了,我猜是状态栏造成的。如何将它的高度添加到我的计算中?

【问题讨论】:

  • 您能否完整地发布您班级的 XML 文件?

标签: android android-actionbar


【解决方案1】:

根据@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.

【讨论】:

    【解决方案2】:

    您可以使用此代码:

    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

    请尝试使用此解决方案并告诉我们它是否有效。 ;)

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 感谢您的建设性评论 - 您是对的。我已经为未来的访问者编辑了答案。
    【解决方案3】:

    最简单的方法是这样的:

    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>
    

    【讨论】:

      【解决方案4】:

      这里有一些可以帮助你的扩展

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多