尝试为每个单独的 ListAdapter 按钮设置自定义字体
我有一个列表适配器,我使用 holder.text.setTypeface(TYPEFACE)
来设置字体,我有一个名称字符串,我使用它为每个 ListView
按钮分配其名称文本。但我想知道如何为一个单独的列表项设置自定义字体。基本上说我有 3 个 ListAdapter 按钮:按钮 1、按钮 2 和按钮 3。我希望按钮 2 是自定义字体,而按钮 1 和按钮 3 保持常规。这怎么能做到呢?
I have a list adapter and im using holder.text.setTypeface(TYPEFACE)
to set the type face and I have a string of names that im using assign each ListView
button its text. But im wondering how to set just one individual list item a custom font. Basically say I have the 3 ListAdapter buttons, Button 1, Button 2, and Button 3. I want Button 2 to be a custom font while Button 1 and Button 3 Stay regular. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建扩展 ArrayAdapter 的类并在 getView 过程中设置字体。
Create class that extends ArrayAdapter and set the font in the getView procedure.
有几种方法可以做到这一点,我最喜欢的两种方法是在 XML 布局中设置它。如果每个“Button2”都需要相同的自定义字体并且您想要的
Typeface
已经是系统的一部分,那么这将起作用。第二种方法是使用
ViewBinder
自定义 ListAdapter。正如 Pinassi 所写,可以通过getView()
来完成,但这有一个缺点,即无法轻松查看数据并将通用 UI 代码与自定义 UI 代码混合在一起。 SimpleAdapter 和 SimpleCursorAdapter 有一个名为ViewBinder
的内部类,其方法为setViewValue
。每个签名因其管理的基础数据而不同。只需重写
setViewValue
即可仅实现您想要手动控制的自定义。例如,R.id.button1
是一个简单的文本绑定,只需让方法返回 false,API 就会为您处理它,但由于我们正在自定义R.id.button2我们将返回true。
customTypeFace
来自系统 Typeface 或项目的 资产文件夹。 Android 使用 TrueType 字体。There are a couple ways to do this my favorite two are to set it in the XML layout. This will work if the same custom font is desired on every "Button2" and if the
Typeface
you want is already part of the system.The second way to do this is to customize a ListAdapter with a
ViewBinder
. As Pinassi wrote, it is possible to do fromgetView()
however this has the drawback of not being able to easily see data and mixes generic UI code with custom UI code. Both SimpleAdapter and SimpleCursorAdapter have an inner class namedViewBinder
with methodsetViewValue
. Each signature is different because of the underlying data it manages.Simply override
setViewValue
for only the customization you want to manually control. For example isR.id.button1
is a simple text binding just have the method return false and the API will handle it for you but since we're customizingR.id.button2
we will return true.customTypeFace
comes from a system Typeface or the project's assets folder. Android uses TrueType Fonts.