设置为Wrap_content时以编程方式获取视图高度

时间:2018-03-31 00:51:41

标签: android android-layout

我有一个布局,它的高度设置为wrap_content。现在我需要在运行时动态获得真正的高度。

我该怎么做?

我试过了:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Tea Cozy | Home</title>
    <link rel="stylesheet" href="./resources/css/style.css">
  </head>
  <body>
    <header>
      <img src="./resources/images/img-tea-cozy-logo.png" alt="our logo">
      <nav>
        <a href="#mission"><span>Mission</span></a>
        <a href="#tea-of-month"><span>Featured Tea</span></a>
        <a href="#locations"><span>Locations</span></a>
      </nav>
        </header>
  <!-- main-content,our mission -->
        <div id="mission">
          <div class="mission-banner">
            <h2>Our Mission</h2>
            <h4>Handpicked, Artisanally Curated, Free Range, Sustainable, Small Batch, Fair Trade, Organic Tea</h4>
          </div>
        </div>
<!-- tea of the month -->
      <h2>Tea of the Month</h2>  <!--ERROR HERE, ITS HIDING BEHIND THE .MISSION DIV -->
      <h4>What's Steeping at The Tea Cozy?</h4>
      <div id="tea-of-month">
        <div class="item">
          <img src="./resources/images/img-berryblitz.jpg" alt="A picture of Fall Berry Blitz Tea">
          <span>Fall Berry Blitz Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-spiced-rum.jpg" alt="A picture of Spiced Rum Tea">
          <span>Spiced Rum Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-donut.jpg" alt="A picture of Seasonal Donuts">
          <span>Seasonal Donuts</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-myrtle-ave.jpg" alt="A picture of Myrtle Ave Tea">
          <span>Myrtle Ave Tea</span>
        </div>
        <div class="item">
          <img src="./resources/images/img-bedford-bizarre.jpg" alt="A picture of Bedford Bizarre Tea">
          <span>Bedford Bizarre Tea</span>
        </div>
      </div>

      <!-- locations section -->
      <div id="locations">
        <h2>Locations</h2>
        <div class="address">
          <h3>Downtown</h3>
          <p>384 West 4th St</p>
          <p>Suite 108</p>
          <p>Portland, Maine</p>
        </div>
        <div class="address">
          <h3>East Bayside</h3>
          <p>3433 Phisherman's Avenue</p>
          <p>(Northwest Corner)</p>
          <p>Portland, Maine</p>
        </div>
        <div class="address">
          <h3>Oakdale</h3>
          <p>515 Crescent Avenue</p>
          <p>Second Floor</p>
          <p>Portland, Maine</p>
        </div>
      </div>
      <div class="contact">
        <h2>The Tea Cozy</h2>
        <h5>contact@theteacozy.com</h5>
        <h5>917-555-8904</h5>
      </div>
      <!-- copyright -->
      <h5 id="copyright">copyright The Tea Cozy 2017</h5>

  </body>
</html>

但这会给我&#34; -2&#34;这是Wrap_Content。

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

就像

一样
setup.exe

而不是这个

viewObj.getHeight():

这将是工作

答案 1 :(得分:0)

您正试图获得尚未绘制的布局高度。

使用下面的代码段来获取布局的高度

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                height = layout.getHeight(); // here get the height of the view
            }
        });

答案 2 :(得分:0)

当您进入视图或布局高度和宽度值时使用代码..

 textView = findViewById(R.id.textCurve); // first object define it not null then getting value.

        Log.d("Height Value:::",""+textView.getHeight());// view control or layout object.

wrap_content并匹配给定为0的父值。