如何以编程方式将字体自定义字体设置为 Spinner 文本?
我的资产文件夹中有一个 ttf 字体文件。我知道如何将它用于文本视图:
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
textview1.setTypeface(externalFont);
我已经在它自己的 xml 文件中定义了对微调器文本的查找(与 Android 中的通常一样):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
我只是无法从代码中引用此文本视图,我总是遇到空指针异常。例如,我尝试过:
TextView spinner_text=(TextView)findViewById(R.id.text1);
spinner_text.setTypeface(externalFont);
即使是在它自己的 xml 中定义的微调器文本,是否也可以选择我的外部字体?
谢谢。
编辑答案:
这有效:
String [] items = new String[2];
items[0]="Something1";
items[1]="Something2";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinaca, items) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
((TextView) v).setTypeface(externalFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
((TextView) v).setTypeface(externalFont);
v.setBackgroundColor(Color.GREEN);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
可能需要添加
import android.view.ViewGroup;
到文件顶部的导入列表中。由于某种原因,当 Eclipse 无法识别代码中涉及的 ViewGroup 类时,它不会提出此建议。
I have a ttf font file in my assets folder. I know how to use it for textviews with:
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
textview1.setTypeface(externalFont);
I have defined look for my spinner text in it's own xml file (as usuall in android):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:textColor="#ffffff"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
I just can't reference this textview from code, i always get null pointer exceptions. E.g. i tried:
TextView spinner_text=(TextView)findViewById(R.id.text1);
spinner_text.setTypeface(externalFont);
Is it possible to select my external font even for my spinner text defined in it's own xml?
Thank you.
EDIT with answer:
This works:
String [] items = new String[2];
items[0]="Something1";
items[1]="Something2";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinaca, items) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
((TextView) v).setTypeface(externalFont);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface externalFont=Typeface.createFromAsset(getAssets(), "fonts/HelveticaNeueLTCom-Lt.ttf");
((TextView) v).setTypeface(externalFont);
v.setBackgroundColor(Color.GREEN);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
It may be necessary to add
import android.view.ViewGroup;
To your list of imports at the top of your file. For some reason Eclipse doesn't make this suggestion when it doesn't recognize the ViewGroup class involved in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这对我有用(使用 CommonsWare 和 gsanllorente 的 答案):
如果您像我一样,最初使用 ArrayAdapter.createFromResource() 和数组资源填充 Spinner(如 Spinner 文档),然后您可以像这样使用 MySpinnerAdapter :
This is what worked for me (using ideas both from CommonsWare's and gsanllorente's answers):
If you, like me, originally populated the Spinner using
ArrayAdapter.createFromResource()
and an array resource (as in Spinner documentation), then you'd use MySpinnerAdapter like this:您可以在
getView()
和getDropDownView()
中通过自己的自定义SpinnerAdapter
应用字体。You would apply the font through your own custom
SpinnerAdapter
, ingetView()
andgetDropDownView()
.如果您在另一个文件中实现适配器,则可以从适配器的构造函数访问“getAssets()”函数,因为您有 Context 作为参数。
If you implement your Adapter in another file, you can access the "getAssets()" function from the constructor of the Adapter, as you have the Context as a parameter.
尝试创建自定义 custom_spinner.xml
创建自定义 CheckedtextView 像这样
实现新布局
Try this create custom custom_spinner.xml
Create custom CheckedtextView like this
implemente the new layout
这是我之前回答的延续:https://stackoverflow.com/a/51100507/787399
步骤 1:声明 item_spinner.xml
步骤 2:声明 item_spinner_dropdown.xml:
步骤 3:在布局中使用 spinner:
[注意:FontTextView 的 id 在布局、spinner item 和 drop 中都是相同的down item]
步骤 4:在 Activity/Fragment 中使用它:
[有关进一步指导,请参阅我的其他帖子:https://stackoverflow.com /a/51077569/787399 和 https://stackoverflow.com/a/22164007/787399 ]
This is the continuation of my previous answer: https://stackoverflow.com/a/51100507/787399
Step 1: declare item_spinner.xml
step 2: declare item_spinner_dropdown.xml:
Step 3: Use spinner in layout:
[Note: id of the FontTextView is same in both the layouts, spinner item and drop down item]
Step 4: use it in the Activity/Fragment:
[ for further guidance see my other post: https://stackoverflow.com/a/51077569/787399 and https://stackoverflow.com/a/22164007/787399 ]
请遵循 FontTextView、FontEditView、FontRadioButton、FontCheckBox 和 FontButton 的基本自定义。
[确切的答案,看完本指南后请看:
https://stackoverflow.com/a/51113022/787399 ]
在 ArrayAdapter 项布局中使用自定义 FontTextView,如下所示:
使用代码:
在各自的 xml 中描述样式和属性:
并
定义更多样式:
在 strings.xml 中提及字体:
并在布局中使用,以节省一些代码和time:
在Android 16级及以上,这一切都被简化了,因为现在你可以将TTF和其他字体资源保存在
/res/font
文件夹中,而不是在资产中。这会删除大部分自定义类、样式和属性,请参阅:Android 中的字体资源
快乐编码! :-)
Please follow basic customization of FontTextView, FontEditView, FontRadioButton, FontCheckBox and FontButton.
[ For the exact answer, after seeing this guide, please see:
https://stackoverflow.com/a/51113022/787399 ]
Use custom FontTextView, in ArrayAdapter item layout, like this:
use the code:
Describe style and attributes in respective xmls:
and
define some more styles:
mention fonts in your strings.xml:
and use in the layouts saving some code and time:
At Android level 16 and above, all this is simplified, because now you can keep TTF and other font resources in
/res/font
folder, rather than in assets. That removes most of the custom classes, styles and attributes, see:Font Resources in Android
Happy Coding with style!! :-)
伙计们,我找到了一个很棒的解决方案,我通过帮助程序包装原始适配器,例如
Use this class SpinnerViewHelper 并且很高兴 使用Android
Lambda 表达式进行编程。
Guys I found an awesome solution, I wrap orignal adapter by helper like
Use this class SpinnerViewHelper and happy progamming with Android
Lambda expression is used.