Android: Enable Scrollbars in Custom View

前端 未结 2 1903
北海茫月
北海茫月 2021-01-03 00:00

I have implemented a custom Layout that extends RelativeLayout. It displays lots of different elements that are created at run-time and is scrollable in both dimensions usin

2条回答
  •  一向
    一向 (楼主)
    2021-01-03 00:04

    Just had this same problem and finally figured it out.

    The "problem" is that you are extending RelativeLayout, which is a subclass of ViewGroup. I found that I had no problem doing what you described in your question to get a custom View to display scrollbars, but when I extended ViewGroup or one of its subclasses, no scrollbars appeared.

    I finally remembered that ViewGroups, by default, don't draw themselves. I added one line to my custom ViewGroup constructor: setWillNotDraw(false);

    What do you know, the scrollbars appeared!

    Hope that helps.

提交回复
热议问题