如何在 xml 或 java 中获取字体文件夹
how to get font folder in xml or java
嗨,我想设置字体,但我有问题
我使用书法库来更改字体
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/IRANSansMobile.ttf")
.build()))
但是这是从 Assets 文件夹中获取字体!
现在我想更改底部导航字体,但现在我无法访问资产文件夹!
<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">@myfont/iransansmobile</item>
</style>
所以这是我的问题:
1.how 我可以在 res/myfont 文件夹的 setDefaultFontPath 中设置字体吗?
2.how 我可以从 Assets 文件夹中设置字体吗?
您好,我从 How to use custom font in android xml?
找到了这个解决方案
package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
XML:
<com.vins.test.MyTextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
祝你好运。
嗨,我想设置字体,但我有问题 我使用书法库来更改字体
new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/IRANSansMobile.ttf")
.build()))
但是这是从 Assets 文件夹中获取字体! 现在我想更改底部导航字体,但现在我无法访问资产文件夹!
<style name="Widget.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
<item name="fontFamily">@myfont/iransansmobile</item>
</style>
所以这是我的问题:
1.how 我可以在 res/myfont 文件夹的 setDefaultFontPath 中设置字体吗?
2.how 我可以从 Assets 文件夹中设置字体吗?
您好,我从 How to use custom font in android xml?
找到了这个解决方案package com.vins.test;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"your_font.ttf");
setTypeface(tf);
}
}
XML:
<com.vins.test.MyTextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="This is a text view with the font u had set in MyTextView class "
android:textSize="30dip"
android:textColor="#ff0000"
>
祝你好运。