效果圖:

如何做?
- 引入自定義字體,如果Android工程里面沒有
assets檔案夾,在Android視圖下可以右鍵app,選擇New -> Folder -> Assets Folder

右鍵assets檔案夾,選擇New -> Folder -> Font Resources Folder

到這里要注意了!需要改下fonts檔案夾的路徑,就是勾上Change Folder Location,填src/main/assets/fonts,不然會按照默認路徑:src/main/res/font/ 建檔案夾的,

最后將字體檔案復制進fonts檔案夾內,格式可以是ttf,也可以是otf

- 代碼使用:
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/包圖小白體.ttf");
mTvContent.setTypeface(tf);
也可以自定義TextView:
public class BTWhiteTextView extends AppCompatTextView {
public BTWhiteTextView (Context context) {
super(context);
init(context);
}
public BTWhiteTextView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public BTWhiteTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context mContext) {
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/包圖小白體.ttf");
setTypeface(tf);
}
}
最后在xml檔案上參考BTWhiteTextView即可:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity">
<com.xz.android72test.ui.widget.BTWhiteTextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:textSize="38sp"
android:textColor="#000"
android:text="須知少時凌云志,\n曾許人間第一流,"/>
</android.support.constraint.ConstraintLayout>
非常感謝你能看到最后,如果能夠幫助到你是我的榮幸,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/234280.html
標籤:其他
下一篇:c語言小程式 祝你生日快樂!
