【问题标题】:Get the visible height of a view获取视图的可见高度
【发布时间】:2015-06-20 20:10:24
【问题描述】:

是否可以在状态栏打开后立即知道我的活动的可见高度?

我想知道我的屏幕的可见高度。

【问题讨论】:

标签: android android-activity


【解决方案1】:

尝试查看 treeobserver 方法.. 喜欢:

   main_layout.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() 
                    {

                        Rect r = new Rect();
                        // r will be populated with the coordinates of your view
                        // that area still visible.
                        main_layout.getWindowVisibleDisplayFrame(r);

                        int heightDiff = main_layout.getRootView().getHeight()-(r.bottom -r.top);

                    }
    });

将 main_layout 替换为您想要的视图对象。 希望对您有所帮助。

【讨论】:

  • 它适用于所有视图高度布局。节省 2 天 :)
  • 很高兴它有帮助..!
  • getWindowVisibleDisplayFrame 是否将状态栏和导航栏高度计入计算?
【解决方案2】:

myLayout.getHeight();只有在它显示在屏幕上之后才会返回视图的实际可见高度,然后它只会是 0。 如果你真的有兴趣了解 myLayout.getHeight() 那么你可能想在视图显示在屏幕上之后检查一下

如果您想知道屏幕的高度/宽度,那么您可以考虑使用以下代码: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);

最终 int 高度 = dm.heightPixels; 最终 int 宽度 = dm.widthPixels;

【讨论】:

    【解决方案3】:

    希望这行代码对你有用。当我想要屏幕的高度 n 宽度时,我会使用它

    // get screen width/height
    
              DisplayMetrics metrics = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(metrics); height =
              metrics.heightPixels; width = metrics.widthPixels;
    

    【讨论】:

      【解决方案4】:

      给你的主要布局元素一个 id:

      <LinearLayout id="myLayout">
          ...
      </LinearLayout>
      

      然后在setContentView()之后得到它的高度:

      View myLayout = (View) findViewById(R.id.myLayout);
      int height = myLayout.getHeight();
      

      另请参阅this question 了解更多信息。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-06
        • 2015-05-22
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 2012-02-22
        • 2014-09-23
        相关资源
        最近更新 更多