2017-07-19 105 views
0

很奇怪的问题, 而我没有找到任何解释它为什么发生。 我有两个非常经典的textViews,我想为每个textViews应用两种不同的字体。 '标题'在正常的'描述'中。 问题是,它只需要第一个并将它应用于它们两个。 说明:如果我把中等或轻的第一个,textviews将具有相同的字体,无论我为第二个字体。 这里是我的xml:Android:两个TextView,两个字体,只有一个应用

<TextView 
      android:id="@+id/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:fontFamily="sans-serif-medium" 
      android:textColor="@color/black" 
      android:textSize="14sp" /> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:textColor="@color/black" 
      android:textSize="12sp" 
      android:fontFamily="sans-serif-light" 
      android:visibility="gone" /> 

结果在媒体是他们两个。 (编辑:第二的TextView的能见度在代码programaticly改变)

我试着programaticly做到这一点:

final TextView tv_title = (TextView) v.findViewById(R.id.title); 
     if (tv_title != null) { 
      tv_title.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL)); 
     } 
final TextView tv_subTitleription = (TextView) v.findViewById(R.id.description); 
      if (tv_subTitleription != null) { 
tv_subTitleription.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); 
       } 

我很认真地通过这个奇怪的态度感到惊讶。有没有人有任何想法,为什么它不应用每个不同的字体?

谢谢:)

+0

你确定两种字体都存在吗?我的Android经验法则是任何字体,我没有.ttf为自己不可能工作。 –

+0

我相信,如果我先将其中一个工作,另一个相同。我在应用程序的其他地方使用这些字体,它的工作方式就像一个魅力... – hermance

回答

1

我建议你创建一个支持自定义字体TextView的。 你可以使用我的代码:

import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class TypefaceTextView extends TextView { 

public TypefaceTextView(final Context context) { 
    super(context); 
} 

public TypefaceTextView(final Context context, final AttributeSet attrs) { 
    super(context, attrs); 
    initAttribute(attrs); 
} 

public TypefaceTextView(final Context context, final AttributeSet attrs, 
     final int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    initAttribute(attrs); 
} 

public void initAttribute(final AttributeSet attrs) { 
    TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(
      attrs, 
      R.styleable.TypefaceTextView, 
      0, 0); 

    try { 
     switch (typedArray.getInteger(R.styleable.TypefaceTextView_typeface, 0)) { 
     case 0: 
      setTypeface(FontUtils.getFirstFont(getContext())); 
      break; 
     case 1: 
      setTypeface(FontUtils.getSecordFont(getContext())); 
      break; 
     default: 
      break; 
     } 
    } finally { 
     typedArray.recycle(); 
    } 
    } 
} 

attrs.xml文件有以下内容添加到文件夹值:

<declare-styleable name="TypefaceTextView"> 
    <attr name="typeface" format="enum"> 
     <enum name="name_typeface_1" value="0"/> 
     <enum name="name_typeface_2" value="1"/> 
    </attr> 
</declare-styleable> 

此操作后,您可以在XML文件中添加字体到TextView的,例如:

<com.example.project.TypefaceTextView 
      android:id="@+id/tvTypeface" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:typeface="name_typeface_1"/> 

代码FontUtils:

public class FontUtils { 

private static final String FONT_1_PATH = "fonts/font1.ttf"; 

private static final String FONT_2_PATH = "fonts/font1.TTF"; 

public static Typeface getFirstFont(Context context) { 
    return getFont(context, FONT_1_PATH); 
} 

public static Typeface getSecordFont(Context context) { 
    return getFont(context, FONT_2_PATH); 
} 

private static Typeface getFont(Context context, String name) { 
    return Typeface.createFromAsset(context.getAssets(), name); 
} 
} 

我认为这是一个灵活的解决方案,因为未来可能需要更多的字体。

+0

不完全是我想要的,但它工作 – hermance

+0

如果您需要帮助,请致电viber或电报+380971493220 @hermance –