【问题标题】:Custom TextView not visible in 5.0 and higher devices自定义 TextView 在 5.0 及更高版本的设备中不可见
【发布时间】:2018-07-10 17:49:57
【问题描述】:

以下是我自定义的 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 代码,但无法理解我在这里做错了什么。

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多