尝试为每个单独的 ListAdapter 按钮设置自定义字体

发布于 2024-10-10 02:05:28 字数 230 浏览 0 评论 0原文

我有一个列表适配器,我使用 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 技术交流群。

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

发布评论

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

评论(2

無心 2024-10-17 02:05:28

创建扩展 ArrayAdapter 的类并在 getView 过程中设置字体。

Create class that extends ArrayAdapter and set the font in the getView procedure.

这个俗人 2024-10-17 02:05:28

有几种方法可以做到这一点,我最喜欢的两种方法是在 XML 布局中设置它。如果每个“Button2”都需要相同的自定义字体并且您想要的Typeface已经是系统的一部分,那么这将起作用。

<TextView 
android:id="@+id/button2"
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:typeface="sans" />

第二种方法是使用 ViewBinder 自定义 ListAdapter。正如 Pinassi 所写,可以通过 getView() 来完成,但这有一个缺点,即无法轻松查看数据并将通用 UI 代码与自定义 UI 代码混合在一起。 SimpleAdapterSimpleCursorAdapter 有一个名为 ViewBinder 的内部类,其方法为 setViewValue。每个签名因其管理的基础数据而不同。

只需重写 setViewValue 即可仅实现您想要手动控制的自定义。例如,R.id.button1 是一个简单的文本绑定,只需让方法返回 false,API 就会为您处理它,但由于我们正在自定义 R.id.button2我们将返回true。

switch(v.getId){
case R.id.button2:
   TextView button2 = (TextView) v;
   button2.setTypeface(customTypeFace);
   // bind text here or return false to allow the API to do this for you.
break;
// other customization
}

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.

<TextView 
android:id="@+id/button2"
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:typeface="sans" />

The second way to do this is to customize a ListAdapter with a ViewBinder. As Pinassi wrote, it is possible to do from getView() 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 named ViewBinder with method setViewValue. 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 is R.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 customizing R.id.button2 we will return true.

switch(v.getId){
case R.id.button2:
   TextView button2 = (TextView) v;
   button2.setTypeface(customTypeFace);
   // bind text here or return false to allow the API to do this for you.
break;
// other customization
}

customTypeFace comes from a system Typeface or the project's assets folder. Android uses TrueType Fonts.

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