安卓:两种文本视图,两种字体,只应用一种



很奇怪的问题, 而且我找不到任何东西可以解释为什么会发生这种情况。 我有两个非常经典的文本视图,我想对每个文本视图应用两种不同的字体。 常规的"标题",灯光的"描述"。 问题是它只需要第一个并将其应用于它们。 说明:如果我将中或浅色放在第一个,则两个文本视图都将具有相同的字体,无论我为第二个字体添加什么字体。 这是我的 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" />

结果是它们都在中等中。(编辑:第二个文本视图的可见性在代码中以编程方式更改(

我试图以编程方式做到这一点:

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

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

谢谢:)

我建议您使用支持字体创建自定义文本视图。 你可以使用我的代码:

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 文件中的文本视图,例如:

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

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

相关内容

最新更新