android 使用外部字体

在assets目录下再建一个fonts文件夹,将字体文件放到里面



单个使用时可以使用此方法:

private void myTypeFace(TextView text){

TypeFace face = TypeFace.createFromAsset(getAssets(),"fonts/HanYi.ttf");

text.setTypeFace(face);

}


如大面积使用或全局使用可再Application中先加载取到对应的TypeFace 

import android.app.Application;
import android.graphics.Typeface;

/**
 * 自定义application
 * Created by Administrator on 2017/9/19 0019.
 */

public class TApplication extends Application{

    public static Typeface textTypeface = null;

    @Override
    public void onCreate() {
        super.onCreate();
        textTypeface = Typeface.createFromAsset(getAssets(), "fonts/huawenxingkai.ttf");
    }
}
自定义textView:

/**
 * 设置字体
 * Created by Administrator on 2016/5/31 0031.
 */
public class MyFontTextView extends AppCompatTextView{
    private Context mContext=null;
    Typeface textTypeface =null;
    public MyFontTextView(Context context) {
        super(context);
        init(context);
    }

    public MyFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public MyFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs,defStyle);
        init(context);
    }

    private void init(Context context) {
        if(mContext==null){
            this.mContext = context;
        }
        if(textTypeface ==null){
            textTypeface = TApplication.textTypeface;
        }
        this.setTypeface(textTypeface);
    }
}
布局文件中引用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context="com.gaof.typefacedemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="系统自带字体效果"
        android:textSize="20sp"
        android:layout_marginTop="10dp"/>
    <com.gaof.typefacedemo.view.MyFontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="外部字体效果"
        android:textSize="20sp"
        android:layout_marginTop="10dp" />

</LinearLayout>
运行后效果图:


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,你可以通过以下步骤来修改字体: 1. 首先,打开Android Studio并进入File菜单。 2. 在File菜单中选择Settings选项,然后选择Editor -> Font选项。 3. 在Font选项中,你可以看到三个不同的字体样式:Primary font,Secondary font和Console font。 - Primary font用于编辑器中的普通文本。 - Secondary font用于编辑器中的代码注释。 - Console font用于终端窗口中的文本。 4. 对于每个字体样式,你可以单击右侧的下拉菜单来选择所需的字体。 5. 在选择字体之后,你可以根据需要调整字体的大小和行间距。 6. 最后,单击Apply按钮以应用所做的更改。 此外,如果你想导入外部字体文件来使用,可以按照以下步骤进行操作: 1. 将你的字体文件(通常是.ttf或.otf格式)复制到项目的资源目录中,例如res/font/。 2. 在XML布局文件或代码中的TextView中使用android:fontFamily属性来引用该字体文件。例如:android:fontFamily="@font/myfont"。 - 其中@font/myfont是指字体文件的路径和名称。 3. 保存并构建你的项目,字体将会被应用到相应的TextView上。 请注意,如果你想自定义Android Studio的编辑器字体,你可以在Settings中找到Appearance & Behavior -> Appearance选项,并选择Override default font by(not recommended)选项。在该选项中,你可以设置字体名称和大小来修改默认字体。 希望以上信息对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android studio中TextView改变字体的两种方式(如仿宋、隶书)](https://blog.csdn.net/lanrenxiaowen/article/details/54410079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android Studio字体的修改](https://blog.csdn.net/lyp_1020k/article/details/78895567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值