#Android 自定义字体样式

Android中自定义字体设置一般通过 facetype属性进行设置,先看一下官网提供的方法

顾名思义 就是说我们可以通过使用项目中assets文件下的资产文件或者是android本身的系统文件进行字体设置。

如果使用assets方法的话,首先我们需要在项目路径下创建assets文件夹,

如图所示,设置好文件之后,可以使用

     Typeface typeface1 = Typeface.createFromAsset(this.getAssets(),"fonts/1.TTF");
     tvText1.setTypeface(typeface1);

方法 或者 自定义一个 TextView的子类

public class MyTextView extends TextView {
public MyTextView(Context context) {
    super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void setTypeface(Typeface tf, int style) {
    super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "2.tff"));
}

}

如果使用系统文件的话,在引用的时候路径设置需要注意路径的设置需要一system开头

Typeface tf = Typeface.createFromFile("/system/fonts/AndroidClock.ttf")

通过这些方法均可以实现自定义的TextView的文本样式

时间: 2024-07-30 01:36:41

#Android 自定义字体样式的相关文章

转:android 自定义RadioButton样式

http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用RadioButton实现了如上效果,其实很简单的. 首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片: Xml代码   <?xml version="1.0" encoding="utf-8"?> &

Android设置字体样式

1.创建assets文件夹:右键单击main目录,选择New>Folder>Assets Folder. 不推荐直接创建文件夹后改名为assets,之前有过文件夹就在这里有时候能够读取到,有时候读取不到的情况 2.assets底下创建一个fonnts文件夹,并放入要使用的字体文件(.ttf),并提供相对路径给createFronAsset()来创建Typeface对象. //得到TextView控件对象 TextView textView =(TextView)findViewById(R.i

android 自定义progressbar 样式

在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 背景 gradient是

Android 自定义字体 例子

1.选择你喜欢的字体,下载字体文件 字体参考:http://www.creativebloq.com/graphic-design-tips/best-free-fonts-for-designers-1233380 2.项目assets文件中新建font文件夹,将你的字体文件放在该文件夹中 3.项目代码 布局代码: [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <Line

Android 自定义RadioButton样式

 上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用RadioButton实现了如上效果,其实很简单的. 首先定义一张background,命名为radio.xml,注意该background必须为xml样式的图片: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.an

Android自定义字体

1.在assets中创建文件夹fonts,放入字体文件.ttf 2.使用实例: [java] view plain copy TextView tv= (TextView)findViewById(R.id.tv); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Lobster.ttf"); tv.setTypeface(tf);

android自定义radiobutton样式文字颜色随选中状态而改变

主要是写一个 color selector 在res/建一个文件夹取名color res/color/color_radiobutton.xml 1 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 2 <item android:state_checked="true" android:color="@color/color_text_select

Android自定义Dialog样式

效果图: 核心代码: package com.zms.toast; import android.app.Dialog; import android.content.Context; import android.content.res.Resources; import android.os.Handler; import android.os.Message; import android.util.DisplayMetrics; import android.view.Gravity;

Android Toolbar 标题居中及字体样式自定义

首先找到 toolbar 所在xml布局文件,将toolbar控件放置在一个相对布局(RelativeLayout)中,然后设置 toolbar 的 title 为空(在 title 属性栏中随便打几个空格即可): 然后在 toolbar 所在的相对布局中加入 TextView 控件,将其 Text 设置为想要的标题: 并将该 TextView 控件设置水平居中以及垂直居即可(即设置 layout_centerHorizontal 和 layout_centerVertical): 最后根据实际