Android 自定义字体样式 及系统默认字体样式 的设置

来源:互联网 发布:adobe cc 2017 mac 编辑:程序博客网 时间:2024/05/22 09:44

Android   能添加文字的控件都可以设置字体样式  

关键字是:Typeface   在Java代码中设置或者在xml文件里面设置都可以


Android系统默认给出四种样式的字体 , 分别是 : Nomal    Monospace   Sans   Serif  

要想使用自定义的字体样式先要从网上下载一个 .tty 格式的字体文件

然后粘贴到项目里面 , 具体步骤如下 :

一    工作区间切换到 Project区间    在 app  src  main 下新建包   assets

      


       再新建fonts文件夹   然后把文件粘贴进去


最终效果如下:

    OK


然后这个是 MainActivity代码:

private String path;    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tv);        path = "fonts" + File.separator + "lcnd.ttf";        AssetManager manager = this.getAssets();        Typeface font = Typeface.createFromAsset(manager , path);        tv.setTypeface(font);    }

下面是Xml代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.administrator.hongyangzv.MainActivity">    <TextView        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:text="Nomal字体"        android:textSize="30dp"        android:typeface="normal"/>    <TextView        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:text="Monospace字体"        android:textSize="30dp"        android:typeface="monospace"/>    <TextView        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:text="Sans字体"        android:textSize="30dp"        android:typeface="sans"/>    <TextView        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:text="Serif字体"        android:textSize="30dp"        android:typeface="serif"/>    <TextView        android:id="@+id/tv"        android:gravity="center"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:text="自定义字体 - 250"        android:textSize="30dp"/></LinearLayout>


----------------------------------------------搞定收工!!!---------------------------------------


切记:路径名字一定要正确, 第一次犯傻 , 浪费一上午 也没找到是哪里出的错!!!被自己气哭了