添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What is the difference between getHeight() and getLayoutParams().height of a View ? I have a View (GoogleAdView) and I want to hide it, I set getLayoutParams().height to zero but the ad's height ( ad.getHeight() ) is not zero.

Is there a way to hide the View so that it doesn't occupy space in the layout?

I've tried to set its visibility to GONE or to set ad.getLayoutParams().height to zero but this doesn't work.

LayoutParams.height is the height you wish your view will have once laid out and could be set to particular constants like WRAP_CONTENT, getHeight() returns the actual height (it returns 0 until the view isn't laid out). See How Android Draws Views and View - Size, padding and margins .

As Michael said, you have to call requestLayout().

The correct way to hide a view and ignore it in layouts is to use

setVisibility(View.GONE);

If this is not working for you, you need to find out why. Trying to tweak the sizes is not a good path.

If you have problems with your layout, post it here.

my problem is that if I do with setVisibility(View.GONE), the space were was ad remains unoccupied and the layout doesn't resize itself. I've tried with ad.refreshDrawableState(); but still the layout doesn't resize... – Buda Gavril Jan 28, 2011 at 8:23 refreshDrawableState() will not help. Did you call refreshLayout() after hiding the view? – Michael Jan 28, 2011 at 8:32 Give android:weight="1" for the layout which is not getting resized and try with setVisibility(View.GONE); for ad view. – Kantesh Jan 28, 2011 at 9:32

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.