android - 自定义 TextView 在 5.0 及更高版本的设备中不可见
问题描述
以下是我的自定义 TextView 类:
public class MontTextView extends android.support.v7.widget.AppCompatTextView{
public MontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MontTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Montserrat-Regular.ttf");
setTypeface(tf ,1);
}
}
在 XML 中,我像这样使用它:
<com.minimalist.gorakh.customviews.MontTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_20sdp"
android:text="Text"
android:textColor="#fff"
android:textSize="@dimen/_25ssp"
app:layout_constraintBottom_toBottomOf="@+id/betaView"
app:layout_constraintStart_toStartOf="@+id/betaView"
app:layout_constraintTop_toTopOf="@+id/betaView" />
我从以前的项目中复制了自定义 TextView 代码,但在这里不知道我在做什么错。
解决方案
尝试这个 :
import android.widget.*;
import android.util.*;
import android.content.*;
import android.graphics.*;
public class MontTextView extends TextView{
public MontTextView (Context context) {
super(context);
init();
}
public MontTextView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MontTextView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Montserrat-Regular.ttf"));
}
}
推荐阅读
- html - 是否可以在不刷新页面的情况下将数据从 node.js express 发送到前端 html?
- javascript - 如何将 ID 传递给 ngx-bootstrap 模态以进行删除确认
- javascript - 我正在尝试计算一组函数的小计,但没有得到任何输出:
- dictionary - mapbox-gl-js 围绕 lat/lng 创建一个扇区?
- java - web.xml 应该在使用 Intellij IDEA IDE 的 Java EE 项目中的哪个位置?
- docker - 如何将 microk8s 与较新的 Docker 一起使用?
- javascript - 是否可以通过 poi_label 过滤建筑物层?
- django - 使用 Twitter Bootstrap 设置 Django 错误样式
- three.js - three.js - 无法读取未定义的属性“y”
- javascript - 图像悬停生成器