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>
- Android SDK 开发范例大全 ---3.14控制不同的文字字体
- Android SDK开发范例大全 ---3.4置换TextView文字
- Android SDK 开发范例大全 ---3.13置换文字颜色的机关
- Android SDK 开发范例大全 ---3.10不同Activity之间的数据传递
- Android SDK开发范例大全 ---3.5取得手机屏幕的大小
- Android SDK 开发范例大全 ---3.7简单的按钮事件
- Android SDK 开发范例大全 ---3.8手机页面的转换
- Android SDK开发范例大全学习笔记——3.1更改宇显示文字标签
- Google Android SDK开发范例大全
- Google Android SDK开发范例大全
- Google Android SDK开发范例大全
- 《Google android SDK开发范例大全》
- Google Android SDK开发范例大全 (一)
- Google Android SDK开发范例大全目录
- 3.14 MonoForAndroid用户人机界面--控制不同的文字字体
- 控制不同文字字体Typeface
- Android SDK开发范例大全 ---3.3更改TextView颜色
- Android SDK 开发范例大全 ---3.9调用另一个Activity
- Java 通过 Jni 调用 C++代码
- Spring模拟aop静态代理案例<一>
- Cocos2d-x-------------------------------------------Label数字文字描边
- Proxy(代理)设计模式
- READING NOTE: Object Detection by Labeling Superpixels
- Android SDK 开发范例大全 ---3.14控制不同的文字字体
- 旧I/O实现的通道之Flip_clear VS 输入输出通道联通
- Linux下安装两个mysql5.5以上版本
- 初识云计算的三种服务模式 (IaaS SaaS PaaS)
- (java)Pascal's Triangle
- myeclipse自动生成实体类
- nginx日志简单认识
- 织梦 channelartlist 按指定的typeid排序[推荐]
- 浏览器渲染原理