字体会在Android Studio预览中更改,但不会在设备中更改
我创建了一个应用于字体会在Android Studio预览中更改,但不会在设备中更改,android,text,coding-style,styles,Android,Text,Coding Style,Styles,我创建了一个应用于TextView的样式。它出现在我想要的Android Studio预览中,但在设备上它是不同的。我能做什么 这是XML代码: <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView style="@style/smalltextstyle" android:text="tex
TextView
的样式。它出现在我想要的Android Studio预览中,但在设备上它是不同的。我能做什么
这是XML代码:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
style="@style/smalltextstyle"
android:text="text here "
/>
</ScrollView>
这是我的风格代码:
<style name="smalltextstyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textAlignment" tools:targetApi="jelly_bean_mr1">center</item>
<item name="android:textSize">23sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
匹配父项
匹配父项
居中
23便士
#000
大胆的
一定是这样
这就是出现的情况
在自定义文本视图中使用下面的类,并在资产文件夹中下载字体
public class MyTextView extends AppCompatTextView {
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() {
if (!isInEditMode()) {
Typeface tf;
switch (getTypeface().getStyle()) {
case Typeface.BOLD:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMedium.otf");
break;
case Typeface.ITALIC:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTBookOblique.otf");
break;
case Typeface.BOLD_ITALIC:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMediumOblique.otf");
break;
case Typeface.NORMAL:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
break;
default:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
break;
}
setTypeface(tf);
}
}
}
xml
public class MyTextView extends AppCompatTextView {
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() {
if (!isInEditMode()) {
Typeface tf;
switch (getTypeface().getStyle()) {
case Typeface.BOLD:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMedium.otf");
break;
case Typeface.ITALIC:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTBookOblique.otf");
break;
case Typeface.BOLD_ITALIC:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirLTMediumOblique.otf");
break;
case Typeface.NORMAL:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
break;
default:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/AvenirBook.otf");
break;
}
setTypeface(tf);
}
}
}
例如:
有视频教程吗?因为我是初学者,我不太懂。嗨,下载像AvenirBook.otf这样的示例字体,将其放在assests文件夹中,并在上面的switch case方法中调用它。