android开发不同字体设置

  开发中控制不同的文字字体主要是对Typeface对象的使用

  因为是简单的demo,都是使用android原生的组件,虽丑,可以学到东西就ok啦!(若有错误或者不足,请各位不吝赐教,谢谢!)

  先看看简单运行的效果吧

      

         

 

即设置俩个button,一个textView,点击不同的按钮时,触发不同的事件。直接上代码

MainActivity.java

 1 public class MainActivity extends Activity implements OnClickListener {
 2 
 3     private Button changeSize;
 4     private Button changeFont;
 5     private TextView tv;
 6 
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         initView();
11     }
12 
13     private void initView() {
14         requestWindowFeature(Window.FEATURE_NO_TITLE);
15         setContentView(R.layout.activity_main);
16         tv = (TextView) findViewById(R.id.tv);
17         changeSize = (Button) findViewById(R.id.changeSize);
18         changeFont = (Button) findViewById(R.id.changeFont);
19         changeSize.setOnClickListener(this);
20         changeFont.setOnClickListener(this);
21     }
22 
23     @Override
24     public void onClick(View v) {
25         switch (v.getId()) {
26         case R.id.changeSize:
27             //使用setTextSize改变字体大小,单位为sp
28             tv.setTextSize(20);
29             break;
30         case R.id.changeFont:
31             //必须时间在assets底下创建一fonts文件夹   放入要使用的字体文件(.ttf),提供相对路径给CreateFromAssets()来创建Typeface对象
32             tv.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/song.ttf"));
33         default:
34             break;
35         }
36     }
37 }

activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context=".MainActivity" >
 6 
 7     <TextView
 8         android:id="@+id/tv"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="@string/hello_world"
12         android:textSize="13sp" />
13 
14     <Button
15         android:id="@+id/changeSize"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:layout_alignParentLeft="true"
19         android:layout_below="@+id/tv"
20         android:layout_marginTop="15dp"
21         android:text="@string/changeSize" />
22 
23     <Button
24         android:id="@+id/changeFont"
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:layout_alignBaseline="@+id/changeSize"
28         android:layout_alignBottom="@+id/changeSize"
29         android:layout_marginLeft="16dp"
30         android:layout_toRightOf="@+id/changeSize"
31         android:text="@string/changeFont" />
32 
33 </RelativeLayout>

  将外部字体文件放在fonts/底下,就可以使用AssetsManage来引用外部资源

  除了使用createFromAsset来构造Typeface外,也可以通过defaultFromStyle来使用android内置的几款Typeface,这里不再赘述,下面是Typeface的类型层次结构,可以看到几个常用的方法。

        

代码下载:https://github.com/SamSarah1/AndroidDemo  

   

 

posted @ 2015-11-02 12:51  SamSarah  阅读(823)  评论(0编辑  收藏  举报