Android SDK 开发范例大全 ---3.14控制不同的文字字体

来源:互联网 发布:365抽奖软件破解版 编辑:程序博客网 时间:2024/04/30 02:57

除了文字颜色之外,与文字对象息息相关的文字大小(Size)与字体(Font)是整个TextView文字实例的最后一站,这里讲延续前一个范例的做法,通过按钮对象的Button.onClickListener来处理改变TextView的字体大小与字体。

TextVoew对象里有许多与字体相关的方法,如使用setTextSize改变文字大小,使用setTypeface来指定文字字体。接下来这个案例将设计两个按钮:第一个控制TextView的字体,第二个控制字体大小。这里会特别解说如果是通过外部资源asses,引用外部的字体文件(True TypeFont),再通过Typeface类的createFromAsset方法,让TextView可通过setTypeface来顺利改变文字。

src/com.helloworld/SActivity.java

package com.helloworld;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class SActivity extends Activity {
    /** Called when the activity is first created. */
private Button mButton1;
private TextView mText;
private int[] mColors;
private int colornum;
private  Button sizeButton;
private  Button fontButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //寻找Button这个ID
        mButton1 = (Button) findViewById(R.id.myButton01);
        mText = (TextView) findViewById(R.id.myTextView01);
        sizeButton = (Button) findViewById(R.id.sizebutton);
        fontButton = (Button) findViewById(R.id.fontbutton);
        sizeButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mText.setTextSize(30);
}
});
        fontButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
/*必须实现在assets低下创建一fonts文件夹
* 必须放入要使用的字体文件(.ttf)
*并提供相对路径给createFromAsset()来创建Typeface对象*/
mText.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf"));

}
});
        //声明并构造一数组数组array来存储欲使用的文字颜色
        mColors = new int[]{
        Color.BLACK, Color.RED, Color.BLUE,
        Color.GREEN, Color.MAGENTA, Color.YELLOW
        };
        colornum=0;
        mButton1.setOnClickListener(new Button.OnClickListener(){


public void onClick(View v) {
// TODO Auto-generated method stub
if(colornum<mColors.length){
mText.setTextColor(mColors[colornum]);
colornum++;
}else{
colornum=0;
}
}


        });
    }
}

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/myTextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Button 
        android:id="@+id/myButton01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_1"
        />
    <Button 
        android:id="@+id/sizebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="变大"
        />
    <Button 
        android:id="@+id/fontbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="换字体"
        />    
</LinearLayout>

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="hello">Hello World!</string>
    <string name="button_1">按我改变颜色</string>
    <string name="app_name">s</string>


</resources>

0 0
原创粉丝点击