Ellipsized, multi-line text in an AppWidget?
What appeared to be a simple requirement is becoming quite a painful process. I need an AppWidget to show some (usually multi-line) text - the text will vary in size, and may be longer than the view allows, so needs ellipsizing in that case. The view will also support resizable widgets in Android v3.1, which again requires that the widget be able to dynamically support content of varying sizes correctly. Easy, you'd think? And if it was a single line TextView, I think it would be...
So initially I created a single Te开发者_JS百科xtView, set the properties to ellipsize it when the text doesn't fit, and came across the bug that means this doesn't work - the text only wraps across two lines.
I then grabbed an open source custom view that ellipsizes correctly with multiple lines and it works fine, but only in an activity. I forgot that AppWidget limits what Views can go in a widget, so this also fails ("Class not allowed to be inflated"). I see no way to use this custom view.
So... the best solution I can think of is to add a series of single-row TextViews to the widget, and iterate through substrings until I find the length that fits in each row, before moving on to the next. i.e. manually render/measure/render the text. There are many downsides to this approach.
Is there a better way to get multi-line ellipsized text into an AppWidget?
To be honest, I think you should step back and rethink what you are wanting to do. Being able to resize widgets is really intended for the new kinds of resizable containers, where there is a clearly variable amount of data available and a well-defined way for the widget to express how it should be shown within the container. If you aren't using those, it is going to be very tricky for you to build a layout that adjusts to provide useful information if the user resizes it significantly.
The old approach that people would take -- of having the app widget available in different cell sizes each with a different layout -- probably still make sense for app widgets that are not using the new stack or list UIs.
I've been working on this exact problem as well, and my solution was to make the widget fixed-size, and use 10 TextViews in a vertical LinearLayout. There doesn't seem to be any other way to do this without having an actual ListView.
I guess it's good enough for now.
精彩评论