新闻中心

EEPW首页>嵌入式系统>设计应用> Android――设置字体的大小与样式

Android――设置字体的大小与样式

作者: 时间:2016-09-12 来源:网络 收藏

一个按钮改变字体的大小,一个按钮改变字体的样式

本文引用地址://m.amcfsurvey.com/article/201609/305028.htm

Activity核心代码:

public class Ex03_14 extends Activity

{

private TextView mText;

private Button sizeButton;

private Button fontButton;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mText=(TextView)findViewById(R.id.mytextview);

sizeButton=(Button) findViewById(R.id.sizebutton);

fontButton=(Button) findViewById(R.id.fontbutton);

sizeButton.setOnClickListener(new View.OnClickListener()

{

public void onClick(View v)

{

mText.setTextSize(20); //设置字体大小为20

}

}

);

fontButton.setOnClickListener(new View.OnClickListener()

{

public void onClick(View v)

{

mText.setTypeface(Typeface.DEFAULT_BOLD,Typeface.ITALIC); //设置系统默认的字体样式

// mText.setTypeface

// (Typeface.createFromAsset(getAssets(),fonts/HandmadeTypewriter.ttf)); //设置自己的字体样式

}

}

);

}

}

说明:必须事先在assets底下创建一个fonnts文件夹,并放入要使用的字体文件(.ttf),并提供相对路径给createFronAsset()来创建Typeface对象。但是Android对字体支持不太好,可能有些字体文件不支持,但也不会报错,只是在运行过程中使用默认的字体样式。另外不仅可通过外部资源来构造Typeface,也可以通过defaultFromStyle使用Android内置的几款Typeface.

int BOLD (Typeface.BOLD)

int BLOD_ITALIC

int ITALIC

int NORMAL

Typeface DEFAULT_BOLD (Typeface.DEFAULT_BOLD)

Typeface DEFAULT

Typeface MONSPACE

Typeface SANS_SERIF

Typeface SERIF

另外说明一点:

Button.OnClickListener相当于一个接口,但看起来,Button本身没有OnClickListener这个接口。

android.view.View.OnClickListener说明OnClickListener这个接口是属于View的。

Button更改成EditText,View也是运行正确的!



关键词:

评论


相关推荐

技术专区

关闭