如何在 Android 中设置字体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10662543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a typeface in Android?
提问by Basim Sherif
I have developed an Andriod RSS reader app.I have used custom listview to list RSS titles and its images.Now I want to change the font of RSS titles.How can I set typeface to my title textview?
我开发了一个 Andriod RSS 阅读器应用程序。我使用自定义列表视图来列出 RSS 标题及其图像。现在我想更改 RSS 标题的字体。如何将字体设置为我的标题文本视图?
Here is my adapter class in which I am setting the title Textview.
这是我在其中设置标题 Textview 的适配器类。
Adapter.java
适配器.java
public class InternationalAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
TextView date = (TextView)vi.findViewById(R.id.artist);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
HashMap<String, String> news = new HashMap<String, String>();
news = data.get(position);
// Setting all values in listview
title.setText(International.Title[position]);
date.setText(International.Date[position]);
imageLoader.DisplayImage(International.image[position], thumb_image);
return vi;
}
}
采纳答案by Aerrow
You can use this,
你可以用这个,
public class InternationalAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
TextView date = (TextView)vi.findViewById(R.id.artist);
//Added Here
Typeface font = Typeface.createFromAsset(
activity.getAssets(),
"fonts/androidnation.ttf");
title .setTypeface(font);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
HashMap<String, String> news = new HashMap<String, String>();
news = data.get(position);
// Setting all values in listview
title.setText(International.Title[position]);
date.setText(International.Date[position]);
imageLoader.DisplayImage(International.image[position], thumb_image);
return vi;
}
}
回答by Aerrow
Try like this
像这样尝试
Typeface font = Typeface.createFromAsset(
getContext().getAssets(),
"fonts/androidnation.ttf");
title .setTypeface(font);
回答by Dhruvil Patel
EditText edittext =(EditText) findViewById(R.id.ed);
String path="F:\MTCORSVA.TTF";
Typeface tf=Typeface.createFromFile(path);
edittext.setTypeface(tf);
you can use this:)
你可以用这个:)
回答by anirudh sharma
Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library
来源:https: //segunfamisa.com/posts/custom-fonts-with-android-support-library
Setting typeface like the one below often does not work (Android Runtime Exception font asset not found)
设置像下面这样的字体通常不起作用(Android Runtime Exception font asset not found)
Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/montserrat_regular.ttf");
and can result into error like this:
并可能导致这样的错误:
java.lang.RuntimeException: Font asset not found fonts/montserrat_regular.ttf
You can therefore use the new way as given here
因此,您可以使用此处给出的新方法
Steps:
脚步:
1. Make a folder in resources folder in your project like res/font
and paste your fonts there
1.在你的项目中的资源文件夹中创建一个文件夹res/font
并将你的字体粘贴到那里
You can alternatively create a font family
您也可以创建一个字体系列
Font family is something that was introduced in Android, and it is used to define a group of fonts and their corresponding style. So the system can determine what font resource to use for regular, bold and italic styles and weight configurations. A typical font family looks like this:
Font family是Android中引入的东西,用于定义一组字体及其对应的样式。因此,系统可以确定用于常规、粗体和斜体样式和粗细配置的字体资源。典型的字体系列如下所示:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- regular -->
<font
android:font="@font/josefinslab_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/josefinslab_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<!-- italic -->
<font
android:font="@font/josefinslab_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/josefinslab_italic"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
Note
笔记
A really important thing to note is that we had to define attributes using both android and app namespaces. The app namespace is what ensures that the feature is backward compatible.
需要注意的一件非常重要的事情是,我们必须同时使用 android 和 app 命名空间来定义属性。app 命名空间确保该功能向后兼容。
2. Using the font programmatically
2. 以编程方式使用字体
Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
myTextView.setTypeface(typeface);
Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library
来源:https: //segunfamisa.com/posts/custom-fonts-with-android-support-library
Hope this helps :)
希望这可以帮助 :)
回答by Khan
use custome textview as given below and set font there
使用下面给出的custome textview并在那里设置字体
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
rotate();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
rotate();
}
public MyTextView(Context context) {
super(context);
init();
rotate();
}
private void rotate() {
// TODO Auto-generated method stub
setSelected(true);
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/TrajanPro-Regular.otf");
setTypeface(tf);
}
}
}
and in your listview row xml means in list_rowlayout add textview as
并在您的 listview 行 xml 中意味着在list_row布局中将 textview 添加为
<YourpackageName.MyTextView
android:layout_marginTop="10dip" android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="22px"
android:textColor="#34A4c5"
android:ellipsize="marquee"
android:maxWidth="220dp"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginRight="10dp"></YourpackageName.MyTextView>
here more property are used u can remove which not required and in your getviewmethod define it as
这里使用了更多属性,您可以删除不需要的属性,并在您的getview方法中将其定义为
MyTextView title = (MyTextView)vi.findViewById(R.id.title);
回答by Chathura Jayanath
You can use this library to change font on any view, only need to pass font file name with the binding xml code. https://github.com/ChathuraHettiarachchi/TypeFaced
您可以使用该库在任何视图上更改字体,只需要通过绑定 xml 代码传递字体文件名。https://github.com/ChathuraHettiarachchi/TypeFaced
1.textview
1.文本视图
2.edittext
2.编辑文本
3.switch
3.开关
4.toggle
4.切换
5.button
5.按钮
6.radiobutton
6.单选按钮
7.checkbox
7.复选框
<com.chootdev.typefaced.TypeFacedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:textView_font="YOUR_FONT_NAME.ttf/>
回答by Prateeksha
We can also use this method:
我们也可以使用这个方法:
t.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
Here monospace is a typeface.
这里的 monospace 是一种字体。