当前位置:网站首页>Use typeface to set the text font of textview
Use typeface to set the text font of textview
2022-07-24 12:30:00 【Sharp surge】
stay Android There is a TextView The color and size of the text , All very simple. , It's also a common basic function . But very few of them set the font of the text , What we want to share today is through TypeFace To set up TextView The text of , There are two in the layout Button, There are two small functions : Change the font and make it bigger .
The core part of the function is mainly two points :
establish assets External resources folder , take ttf The font file in this format is placed in this directory
adopt TypeFace Class createFromAsset Method , Give Way TextView adopt setTypeFace To change the font
The complete source code is as follows :
1、 Lord Activity, Pay attention to the comments in the code :
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.memorydemo.R;
public class ChangeFontActivity extends Activity implements View.OnClickListener {
private TextView textViewFont;
@Override
protected void onCreate(Bundle onSavedInstance) {
super.onCreate(onSavedInstance);
setContentView(R.layout.change_textview_font);
textViewFont = findViewById(R.id.textViewFont);
Button btnChangeFont = findViewById(R.id.buttonChangeFont);
Button btnAmplify = findViewById(R.id.buttonAmplify);
btnChangeFont.setOnClickListener(this);
btnAmplify.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonChangeFont:
// Here I am from Android framework I picked any font in the directory
textViewFont.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/BitMDL2.ttf"));
// Direct will ttf Files in assets It's OK under the directory
// textViewFont.setTypeface(Typeface.createFromAsset(getAssets(), "BitMDL2.ttf"));
break;
case R.id.buttonAmplify:
// The default size in the layout is 12 SP, Here, set the font size to 24 SP, Easy to see
textViewFont.setTextSize(24);
break;
default:
break;
}
}
}The storage directory structure of font file is as follows (ttf Font files can be placed directly in assets Catalog , It can also be in assets Next, create a new directory , For example, I built a floor fonts Catalog ):

2、 Layout file change_textview_font.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
3、 The renderings are as follows ( First click to make it bigger 、 Change the font again ):

About Android The font of , There are two points to note :
- The font file must be ttf(True Type Font) Format , otherwise , Even if the program compiles correctly , The font cannot be changed at run time
- at present Android There are some problems with supporting font files , That is, an unsupported font is used ,Android And there will be no mistakes , It's in the default font Droid Sans Replace . So if you use an external font and find that it doesn't change , The problem is Android The font is not supported , Not a program error
External fonts are used in this function , You can also call Typeface Static method of defaultFromStyle, Use native Fonts , It has the following constants , All are int type :
- BOLD
- BOLD_ITALIC
- DEFAULT
- DEFAULT_BOLD
- ITALIC
- MONOSPACE
- NORMAL
- SANS_SERIF
- SERIF
If you are interested, you can test the effect of these fonts .
边栏推荐
- 【功能测试】项目的测试——登录和发布文章功能
- Buckle practice - 27 score after turning the matrix
- 3、 Implementation principle of MFC message mapping mechanism
- QT notes - sort a column specified by qtablewidget
- 三层交换机配置MSTP协议详解【华为eNSP实验】
- Understand what goals the MES system can achieve
- OpenCV:08图像金字塔
- Source code analysis sentry user behavior record implementation process
- Zhihuihuayun | cluster log dynamic collection scheme
- 从零实现深度学习框架——再探多层双向RNN的实现
猜你喜欢

突破内存墙能带来什么?看火山引擎智能推荐服务节支增效实战

Use abp Zero builds a third-party login module (III): web side development
Learn some programming: anti unemployment "vaccine"

Counter attack dark horse: devdbops training, give you the best courses!
![Error: [synth 8-439] module 'xxx' not found not found error solution](/img/47/bb03cc26e254332bf815c80bafb243.png)
Error: [synth 8-439] module 'xxx' not found not found error solution

三层交换机配置MSTP协议详解【华为eNSP实验】

使用TypeFace设置TextView的文字字体

ThinkPHP realizes database backup

QT | summary of the use of edit box
![[data mining engineer - written examination] sheen company in 2022](/img/67/c61dfce8c397ab55fab835b6e81a5f.jpg)
[data mining engineer - written examination] sheen company in 2022
随机推荐
QT notes - qtxml
try...finally总结
Slow motion animation, window related data and operations, BOM operations [DOM (V)]
leetcode:51. N 皇后
Wechat applet generates QR code
Wechat official account development: Material Management (temporary and permanent)
Reserved instances & Savings Plans
TypeNameExtractor could not be found
Quick check list of various XSS payloads
做自媒体视频剪辑有免费可商用的素材网站吗?
Most after analyze table in PostgreSQL_ common_ Why is the elems field not filled in?
Qt5.12 + vs2019 cannot locate the program input point in the dynamic link library
利用huggingface模型翻译英文
Pushgateway installation and Prometheus configuration
微信小程序-绘制仪表盘
Day5: construct program logic
Buckle practice - sum of 34 combinations
Snowflake algorithm (PHP)
Seckill implementation diagram
Guys, do you need to configure anything to use rocksdb when using flinksql? Or do you need any jar packages