首页 > 解决方案 > 自定义 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" />

这在棒棒糖前(v-19)中工作,如下所示: 前棒棒糖

在棉花糖模拟器上: 在此处输入图像描述

我从以前的项目中复制了自定义 TextView 代码,但在这里不知道我在做什么错。

标签: androidandroid-layouttextview

解决方案


尝试这个 :

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"));
    }

}

推荐阅读