Android TextView 设置字体大小的方法
作者:gisoracle 时间:2022-06-18 03:05:25
废话不多说了,直接给大家贴代码了,具体代码如下所示:
package com.example.yanlei.yl4;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView edit;
Button sendBu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (TextView) findViewById(R.id.textView);
sendBu = (Button) findViewById(R.id.button);
sendBu.setOnClickListener(new ButtonClickListener());
}
private class ButtonClickListener implements View.OnClickListener {
public void onClick(View v) {
//System.exit(0);
edit.setText("闫磊我爱你");
edit.setTextColor(Color.BLUE);
edit.setText("这是我的第一个TextView,嘿嘿", TextView.BufferType.EDITABLE);
/**
* 要设置文本的背景色,
* 必须将文本设置成BufferType.SPANNABLE,BufferType.EDITABLE
*/
Spannable sp = (Spannable) edit.getText();
//设置红色背景
sp.setSpan(new BackgroundColorSpan(Color.RED), 3, 8,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),
0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置斜体
sp.setSpan(new AbsoluteSizeSpan(80), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置字体大小
edit.setText(sp);
//edit.setHeight(60);
//android.os.Process.killProcess(android.os.Process.myPid());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
以上代码内容是小编给大家分享的Android TextView 设置字体大小的方法,希望对大家有所帮助。
标签:android,textview,字体,大小
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用log4net记录日志
2022-12-04 03:08:35
![](https://img.aspxhome.com/file/2023/8/106448_0s.jpg)
浅谈Maven镜像更换为阿里云中央仓库(精)
2022-08-06 04:48:17
C#中的char、string和StringBuilder的使用详解
2022-12-12 12:59:50
![](https://img.aspxhome.com/file/2023/8/119608_0s.png)
对指定的网页进行截图的效果 C#版
2022-07-04 03:14:18
openFeign服务之间调用保持请求头信息处理方式
2022-11-07 23:45:21
C#中Request.Cookies 和 Response.Cookies 的区别分析
2023-07-10 07:24:59
Spring Batch轻量级批处理框架实战
2023-01-08 00:24:23
![](https://img.aspxhome.com/file/2023/0/89500_0s.png)
C# 实现TXT文档转Table的示例代码
2022-04-23 07:47:52
利用 filter 机制给静态资源 url 加上时间戳,来防止js和css文件的缓存问题
2022-03-16 07:51:24
![](https://img.aspxhome.com/file/2023/3/116183_0s.png)
java中File类的使用方法
2023-09-11 11:49:55
Java Thread.currentThread().getName() 和 this.getName()区别详解
2021-10-31 01:46:18
![](https://img.aspxhome.com/file/2023/4/61834_0s.png)
springboot publish event 事件机制demo分享
2022-11-19 23:45:45
基于C#技术实现身份证识别功能
2023-10-01 14:16:26
![](https://img.aspxhome.com/file/2023/8/91358_0s.png)
SpringBoot中发送QQ邮件功能的实现代码
2022-09-25 10:19:59
![](https://img.aspxhome.com/file/2023/8/72648_0s.png)
c# xml转word的实现示例
2023-01-05 15:47:59
![](https://img.aspxhome.com/file/2023/6/84416_0s.png)
C#中倒序输出字符串的方法示例
2023-10-27 21:45:13
![](https://img.aspxhome.com/file/2023/8/102268_0s.png)
C#中使用ADOMD.NET查询多维数据集的实现方法
2023-10-27 05:38:15
为什么阿里要慎重使用ArrayList中的subList方法
2023-12-07 19:03:34
![](https://img.aspxhome.com/file/2023/0/82380_0s.jpg)
java项目中的多线程实践记录
2023-10-16 16:12:03
浅谈JVM中的JOL
2022-10-06 15:44:49