Android除了能够改变字体大小,还能改变字体样式。方法非常简单只需要将相应的字体复制到assets目录边可以实现。
步骤如下:
1.在准备字体,在windows/fonts随便拷贝一个字体将其粘贴到eclipse相应项目的assets目录中,assets目录可以存放任何资源文件,以视区别新建一个fonts目录用与存放刚才复制的字体。
2.编辑main.xml,添加一个TextView和两个按钮,分别设置ID
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/tvText" android:id="@+id/tvText"/> <Button android:layout_width="wrap_content" android:id="@+id/btnChangeFontStyle" android:layout_height="wrap_content" android:text="@string/btnChangeStyle_Text"></Button> <Button android:layout_width="wrap_content" android:id="@+id/btnChangeFontSize" android:layout_height="wrap_content" android:text="@string/btnFontSize_Text"></Button> </LinearLayout>
3.编写相应改变字体和大小代码
package gphone.ex03_13;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class EX03_13 extends Activity {
TextView tvText=null;
Button btnChangeFontStyle=null;
Button btnChangeFontSize=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvText=(TextView)findViewById(R.id.tvText);
btnChangeFontStyle=(Button)findViewById(R.id.btnChangeFontStyle);
btnChangeFontSize=(Button)findViewById(R.id.btnChangeFontSize);
btnChangeFontStyle.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tvText.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/STCAIYUN.TTF"));
}});
btnChangeFontSize.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
tvText.setTextSize(50);
}});
}
}
浙公网安备 33010602011771号