【问题标题】:Get view width/height or font width/height/other sizes before first display在首次显示之前获取视图宽度/高度或字体宽度/高度/其他尺寸
【发布时间】:2011-03-21 09:12:51
【问题描述】:

我知道为放置进行视图大小计算是不受欢迎的。 Android 布局管理器应正确放置所有视图。 但是,当进行某种计算时,有一些合法的情况 应该这样做以提供提示(例如 GridView 中的列数 或调整文本视图的大小以允许在一行中显示最少数量的字符)。 对于这些情况,我想获得相关的视图宽度/高度信息或 字体宽度/高度/等。 但是 getWidth()/getHeigth()/etc 调用永远不会返回有效值 除非视图已在显示器上呈现,但为时已晚。 这是第 22 条规定的情况。 我想知道是否有一些我没有找到的方法(android api很大 理解所有我的想法)这使得我上面描述的成为可能。

【问题讨论】:

    标签: android view measurement


    【解决方案1】:

    在计算其父视图之前无法计算视图的大小,但您始终可以通过在其上调用measure(allowedWidth, allowedHeight) 来强制进行此计算。然后getMeasuredWidth() 将起作用。

    http://developer.android.com/reference/android/view/View.html#measure(int, int)

    【讨论】:

      【解决方案2】:

      是的,我测试了这个建议并且它有效!

      示例代码。

      Display display = getWindowManager().getDefaultDisplay();
      View s = findViewById(R.id.Screen); // Screen is a container layout
      s.measure(display.getWidth(), display.getHeight());
      View c = findViewById(R.id.Cell0);
      Log.i(TAG, "cell width = " + String.valueOf(c.getMeasuredWidth())
        +  " height = " + String.valueOf(c.getMeasuredHeight()));
      

      我不明白我是怎么错过的! :) 我对测量过程了如指掌,但我一直坚持使用它 在自定义视图中...

      【讨论】:

      • display 来自哪里?
      猜你喜欢
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      相关资源
      最近更新 更多