Android: GridView width not wrapping to content?

前端 未结 4 1759
广开言路
广开言路 2021-01-13 15:15

I am trying to display a GridView in a Dialog. Despite all my efforts, the GridView width grows to the entire screen, instead of wrapping to the columns. The layout and an

4条回答
  •  孤街浪徒
    2021-01-13 15:37

    I know that this post is a bit outdated. But if someone needs a solution to this, this answer may come in handy.

    It is possible to set the view's width after screen measurement.

    To do this:

    Let your class implement the OnGlobalLayoutListener. The screen is measured when the onGlobalLayout method is called. We can do our magic here. GridView.getLayoutParams().width = ....

    edit: I wasn't very clear on how to add the onGlobalLayoutListener. See plugmind's post, he shows how to add it. Can't figure out how to get view/layout width/height

    Kind regards, Bram

提交回复
热议问题