2017-07-19 104 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 –