AppWidget 中的椭圆形多行文本?

发布于 2024-11-07 23:18:02 字数 699 浏览 2 评论 0原文

看似简单的需求正在变得相当痛苦的过程。我需要一个 AppWidget 来显示一些(通常是多行)文本 - 文本的大小会有所不同,并且可能比视图允许的长度更长,因此在这种情况下需要省略。该视图还将支持 Android v3.1 中可调整大小的小部件,这再次要求该小部件能够正确动态地支持不同大小的内容。你觉得很容易吗?如果它是单行 TextView,我认为它会是......

所以最初我创建了一个 TextView,设置属性以在文本不适合时省略它,并遇到 该错误意味着这不起作用 - 文本仅换行两行。

然后,我抓取了一个开源自定义视图,可以正确省略多行它工作得很好,但仅限于一项活动。我忘记了 AppWidget 限制了小部件中可以包含的视图,因此这也失败了(“不允许类膨胀”)。我看不出有什么办法可以使用这个自定义视图。

所以...我能想到的最好的解决方案是向小部件添加一系列单行 TextView,并迭代子字符串,直到找到适合每行的长度,然后再继续下一行。即手动渲染/测量/渲染文本。这种方法有很多缺点。

有没有更好的方法将多行省略号文本放入 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 TextView, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-11-14 23:18:02

说实话,我认为你应该退后一步,重新考虑一下你想要做什么。能够调整小部件的大小实际上是针对新型可调整大小的容器,其中可用的数据量明显可变,并且小部件有明确定义的方式来表达它应该如何在容器中显示。如果您不使用这些,那么构建一个布局来调整以在用户大幅调整布局大小时提供有用的信息将非常棘手。

人们采用的旧方法——让应用程序小部件以不同的单元格大小可用,每个单元格大小都有不同的布局——对于不使用新堆栈或列表 UI 的应用程序小部件可能仍然有意义。

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.

情绪少女 2024-11-14 23:18:02

我也一直在研究这个确切的问题,我的解决方案是使小部件固定大小,并在垂直 LinearLayout 中使用 10 个 TextView。如果没有实际的 ListView,似乎没有其他方法可以做到这一点。

我想现在已经足够好了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文