AutocompleteTextview 和 CompletionHintView
我目前正在使用 AutocompleteTextView
组件实现自动完成字段。
我正在尝试添加带有结果数量的完成提示,并且只想将其样式设置为与下拉列表元素不同。 组件上有一个名为 completionHintView
的属性,但每次我给它一个之前定义的布局时,它都会抛出 NullPointerException
。
有人有关于如何设置完成提示样式的工作示例吗?
I'm currently implementing an autocomplete field by using the AutocompleteTextView
component.
I'm trying to add a completion hint with the number of results, and just want to style it differently from the dropdown list element.
There is an attribute named completionHintView
on the component, but every time I give it a layout I previously defined it throws a NullPointerException
.
Does anyone have a working example on how to style the completion hint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能详细说明一下您想要应用哪种样式吗?
如果只是基本的文本样式,您可能可以构建一个 Spannable 并使用结果设置完成提示,因为它接受 CharSequence 。 这篇文章。
如果您正在寻找一种实际操作
TextView
参数的方法(例如填充),AutoCompleteTextView
的源代码 似乎提供了提示(双关语)。这表明 Android 在您指定的资源引用中查找 id
text1
。这种资源的最基本版本只包含具有此 id 的TextView
:将上面的内容保存在布局文件中(例如
completion_hint_view.xml
)并按如下所示引用它AutoCompleteTextView
:第二个选项可能是最容易使用的,并且可以让您完全访问 TextView 的参数。如果您需要对此视图中的文本应用多种样式,您可以合并第一个建议,因为这将为您提供更大的灵活性。
如果这些建议都不够,我可以想到一些不太优雅的解决方法,可能会让您获得相同的结果。
Can you elaborate on what kind of styling you are seeking to apply?
If it's just basic text styling, you could probably build a
Spannable
and set the completion hint with the result, since it accepts aCharSequence
. An example of building aSpannable
and apply styles to it is illustrated in this post.If you're looking for a way to actually manipulate the parameters of the
TextView
(e.g. padding), the source code ofAutoCompleteTextView
seems to provide a hint (pun intended).This reveals that Android looks for the id
text1
in the resource reference you specify. The most basic version of such a resource would contain nothing but aTextView
with this id:Save above in a layout file (e.g.
completion_hint_view.xml
) and reference it as follows from yourAutoCompleteTextView
:This second option is probably the easiest to use and will give you full access to the TextView's parameters. If you need to apply multiple styles to the text in this view, you can incorporate the first suggestion, as that will give you more flexibility.
If neither of these suggestions suffice, I can think of some less elegant work arounds that would probably allow you to get the same result.