zoukankan      html  css  js  c++  java
  • Android改变字体方法——Typeface

    Android除了能够改变字体大小,还能改变字体样式。方法非常简单只需要将相应的字体复制到assets目录边可以实现。

    步骤如下:

    1.在准备字体,在windows/fonts随便拷贝一个字体将其粘贴到eclipse相应项目的assets目录中,assets目录可以存放任何资源文件,以视区别新建一个fonts目录用与存放刚才复制的字体。

    image

    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);
    
    				
    			}});
            
            
        }
    }
  • 相关阅读:
    DS博客作业02--栈和队列
    DS博客作业01-线性表
    c博客06-结构体&文件
    C博客作业05--2019-指针
    C语言博客作业04——数组
    C语言博客作业03--函数
    面向对象设计大作业第二阶段:图书馆系统
    JAVA程序设计-有理数类的设计
    DS博客作业05--查找
    DS博客作业04--图
  • 原文地址:https://www.cnblogs.com/AlexCheng/p/2120042.html
Copyright © 2011-2022 走看看