Android改变字体方法——Typeface
2011-04-06 10:27
375 查看
Android除了能够改变字体大小,还能改变字体样式。方法非常简单只需要将相应的字体复制到assets目录边可以实现。
步骤如下:
1.在准备字体,在windows/fonts随便拷贝一个字体将其粘贴到eclipse相应项目的assets目录中,assets目录可以存放任何资源文件,以视区别新建一个fonts目录用与存放刚才复制的字体。
2.编辑main.xml,添加一个TextView和两个按钮,分别设置ID
3.编写相应改变字体和大小代码
步骤如下:
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); }}); } }
相关文章推荐
- Android改变字体方法——Typeface
- Android 设置字体的三种方法(TypeFace)
- Android 全局改变字体大小的方法
- Android系统状态栏字体颜色在暗色背景和亮色背景下的改变方法
- Android TextView 改变字体方法
- Android TextView 在java代码中改变字体的颜色的方法
- Android开发笔记 改变字体颜色的三种方法
- android radiobutton选中字体颜色改变的方法
- android改变字体的颜色的三种方法
- Android TextView 在java代码中改变字体的颜色的方法
- android改变字体的颜色的三种方法
- Android 设置字体的三种方法(TypeFace)
- android改变字体的颜色的三种方法
- Android开发笔记 改变字体颜色的三种方法
- Android TextView 在java代码中改变字体的颜色的方法
- Android Studio设置、改变字体和主题的方法
- Android开发笔记——改变字体颜色的三种方法
- Android 改变字体颜色的三种方法
- Android开发改变字体颜色方法
- Android 中使用自定义字体的方法