【问题标题】:How to get heights of a transparent softkeys bar and transparent status bar?如何获得透明软键栏和透明状态栏的高度?
【发布时间】:2017-03-08 19:18:44
【问题描述】:

我有一个包含 RecyclerView 和透明状态栏(包含 wifi 信号等...)和软键栏的活动。当然,RecyclerView 中的项目数量是未定义的,我需要第一个具有标准上边距加上状态栏高度,最后一个具有标准下边距加上软键栏的高度。这是必需的,否则当滚动条分别位于顶部或底部时,我将得到第一个和最后一个项目部分被条覆盖。

这是我如何以编程方式处理边距的示例代码:

DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        topSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        bottomSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        standardSpace = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        topSpace.setMargins(     (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 24, metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics));

        bottomSpace.setMargins(  (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8 + 56, metrics) +
                                         getSoftkeysHeight(activity));

        standardSpace.setMargins((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics),
                                 (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8,    metrics));

我正在尝试使用those answers,但它们似乎对我没有帮助,因为屏幕的可绘制表面确实是整个屏幕...

【问题讨论】:

    标签: android android-recyclerview android-statusbar


    【解决方案1】:

    您可以使用RecyclerView.ItemDecoration 修改特定项目的边距。

    1. 创建一个扩展 RecyclerView.ItemDecoration 的类:

      public static class SimpleItemDecorator extends RecyclerView.ItemDecoration {
      
      private int regularMargin;
      private int lastPosition;
      private int statusBarHeight;
      private int navigationBarHeight;
      
      public SimpleItemDecorator(Activity activity, int recyclerViewItemsCount) {
          // get the regular margin
          regularMargin = activity.getResources().getDimensionPixelSize(R.dimen.regularMargin);
      
          // determine the last position
          lastPosition = recyclerViewItemsCount - 1;
      
          // get the height of the status bar
          final Rect rectangle = new Rect();
          final Window window = activity.getWindow();
          window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
          statusBarHeight = rectangle.top;
      
          // get the height of the navigation bar
          final int redId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
          navigationBarHeight = (redId > 0) ? resources.getDimensionPixelSize(resId) : 0;
      
      }
      
      @Override
      public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
          // modify the margins
          final int position = parent.getChildAdapterPosition(view);
          if (position == 0) {
              outRect.top = statusBarHeight + regularMargin;
          } else if (position == lastPosition) {
              outRect.bottom = navigationBar + regularMargin;
          }
      }
      

      }

    2. 将创建的ItemDecorator 添加到 RecyclerView :

      final int recyclerViewItemsCount = 6;
      final SimpleItemDecorator itemDecorator = new SimpleItemDecorator(this, recyclerViewItemsCount);
      recyclerView.addItemDecorator(itemDecorator);
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      相关资源
      最近更新 更多