【问题标题】:Android : two TextView, Two fonts , only one appliedAndroid:两个TextView,两种字体,只应用了一种
【发布时间】:2017-07-20 01:09:08
【问题描述】:

很奇怪的问题, 而且我找不到任何东西来解释它为什么会发生。 我有两个非常经典的 textViews,我想为每个 textViews 应用两种不同的字体。 常规中的“标题”,浅色中的“描述”。 问题是它只需要第一个并将其应用于它们两个。 说明:如果我将中等或浅色放在第一个,两个文本视图将具有相同的字体,无论我为第二个放置什么字体。 这是我的 xml:

<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 的可见性在代码中以编程方式更改)

我尝试以编程方式进行:

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

我对这种奇怪的态度感到非常惊讶。有谁知道为什么不对每个字体应用不同的字体?

谢谢你:)

【问题讨论】:

  • 你确定这两种字体都存在吗?我使用 android 的经验法则是我自己没有 .ttf 的任何字体都不太可能工作。
  • 我敢肯定,如果我先放一个,它会起作用,另一个也一样。我在应用程序的其他地方使用这些字体,它就像一个魅力......

标签: android


【解决方案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 文件添加到 values 文件夹,内容如下:

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

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

【讨论】:

  • 如果您需要帮助,请通过 viber 或电报 +380971493220 @hermance 给我打电话
猜你喜欢
  • 2012-02-18
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多