自定义 Textview 字体不起作用
Custom Textview Font not working
我想在文本视图上使用其他字体。我已经创建了自定义文本视图 class 并创建了字体 folder.The 问题是字体在 compiled.I 之后没有改变 不知道我犯了什么错误 make.I 希望你们能帮我解决这个问题 problem.Thank你提前
activity_example_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.khoi.androidexample.example.Example_One_Activity">
<com.khoi.androidexample.custom.TextViewBold
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/texview_bold_id"
android:text="Hello World!"/>
<com.khoi.androidexample.custom.TextViewMedium
android:layout_width="match_parent"
android:id="@+id/textview_medium_id"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
Example_One_Activity.java
public class Example_One_Activity extends AppCompatActivity {
TextViewBold textViewBold;
TextViewMedium textViewMedium;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example_one);
textViewBold = (TextViewBold) findViewById(R.id.texview_bold_id);
textViewMedium = (TextViewMedium) findViewById(R.id.textview_medium_id);
}
}
TextViewBold.java
public class TextViewBold 扩展了 TextView {
public TextViewBold(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init(context);
}
public TextViewBold(Context context,AttributeSet attrs){
super(context,attrs);
init(context);
}
public TextViewBold(Context context){
super(context);
init(context);
}
private void init(Context context) {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
setTypeface(tf);
}
}
}
您的地址缺少 "fonts/",应该是这样的:
setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/Lato_Medium.ttf"));
在您的 init(Context context)
中传递上下文
和 getAssets() 类似 --> context.getAssets()
Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
isInEditMode() -->确保它将 return true
我re-download字体样式并替换字体文件夹中的文件,它worked.Thank非常感谢您帮助解决了这个问题
我想在文本视图上使用其他字体。我已经创建了自定义文本视图 class 并创建了字体 folder.The 问题是字体在 compiled.I 之后没有改变 不知道我犯了什么错误 make.I 希望你们能帮我解决这个问题 problem.Thank你提前
activity_example_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.khoi.androidexample.example.Example_One_Activity">
<com.khoi.androidexample.custom.TextViewBold
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/texview_bold_id"
android:text="Hello World!"/>
<com.khoi.androidexample.custom.TextViewMedium
android:layout_width="match_parent"
android:id="@+id/textview_medium_id"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</LinearLayout>
Example_One_Activity.java
public class Example_One_Activity extends AppCompatActivity {
TextViewBold textViewBold;
TextViewMedium textViewMedium;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example_one);
textViewBold = (TextViewBold) findViewById(R.id.texview_bold_id);
textViewMedium = (TextViewMedium) findViewById(R.id.textview_medium_id);
}
}
TextViewBold.java
public class TextViewBold 扩展了 TextView {
public TextViewBold(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init(context);
}
public TextViewBold(Context context,AttributeSet attrs){
super(context,attrs);
init(context);
}
public TextViewBold(Context context){
super(context);
init(context);
}
private void init(Context context) {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
setTypeface(tf);
}
}
}
您的地址缺少 "fonts/",应该是这样的:
setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/Lato_Medium.ttf"));
在您的 init(Context context)
中传递上下文和 getAssets() 类似 --> context.getAssets()
Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
isInEditMode() -->确保它将 return true
我re-download字体样式并替换字体文件夹中的文件,它worked.Thank非常感谢您帮助解决了这个问题