欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TextView 设置字体大小的方法

程序员文章站 2024-02-19 09:40:28
废话不多说了,直接给大家贴代码了,具体代码如下所示: package com.example.yanlei.yl4; import android.graphi...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

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 设置字体大小的方法,希望对大家有所帮助。