Android 文本(字体)在较旧的API上看起来褪色

Android 文本(字体)在较旧的API上看起来褪色,android,textview,android-typeface,Android,Textview,Android Typeface,我使用的是一种自定义字体,它在较新的android版本(在API 17(华硕标签)、18(戴尔标签)、19(Nex4)设备上测试)上显示得非常完美。然而,在旧版本(API 8(SE X10i)、10(LG P500H)上,相同的字体看起来褪色(可能变形?) 如果我的解释不合理,这里有一个比较: 关于nex4: 在x10i上: 我使用的是自定义字体。粗体: tvTitle.setTypeface(titleAndBodyFont, Typeface.BOLD); 和身体(“外观*”部分):

我使用的是一种自定义字体,它在较新的android版本(在API 17(华硕标签)、18(戴尔标签)、19(Nex4)设备上测试)上显示得非常完美。然而,在旧版本(API 8(SE X10i)、10(LG P500H)上,相同的字体看起来褪色(可能变形?)

如果我的解释不合理,这里有一个比较:

关于nex4:

在x10i上:

我使用的是自定义字体。粗体:

tvTitle.setTypeface(titleAndBodyFont, Typeface.BOLD);
和身体(“外观*”部分):

标题的XML声明:

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/margin_title_side"
    android:layout_marginRight="@dimen/margin_title_side"
    android:layout_marginTop="@dimen/margin_title_top"
    android:ellipsize="end"
    android:maxLines="1"
    android:textColor="@color/constant_color"
    android:textSize="@dimen/text_size_title" />

tvBody
以类似的方式声明

这是已知的bug吗?如果是的话,有人能帮我找到bug报告吗?这将有助于了解这是固定在哪个版本。如果没有,我将非常感谢找到解决办法


谢谢大家。

我真的帮不了你什么忙,看起来确实像个bug,但是要检查bug扩展,你可能会发现这个测试很有用:

        public class TestBoldText extends View {

            private TextPaint mPaint;

            public TestBoldText(Context context, AttributeSet attrs) {

                super(context, attrs);

                mPaint = new TextPaint(); 


                mPaint.setAntiAlias(true);
                mPaint.setFakeBoldText(true);
                mPaint.setFlags(mPaint.getFlags()|Paint.SUBPIXEL_TEXT_FLAG);

            }

            @Override
            protected void onDraw(Canvas canvas) {

                super.onDraw(canvas);
                canvas.drawText("i am a bold text!", 0, 0, mPaint)

            }

        }
看起来Android使用了一种“假粗体”的方法来绘制粗体文本,而不是特定的粗体字体。我想他们用像素偏移量绘制相同的文本。这个测试在我使用API>=ICS(三星galaxy/amazon kindle)的设备上绘制了可接受的粗体文本,但我很想知道它是否也在您的设备上失败,就像我正在做的wysiwig编辑器一样

另外,
mPaint.setStrokeWidth
应更改文本行宽度。我想使用setStrokeWidth/setAntiAlias/setFakeBoldText可以在较旧的API中获得一个不错的粗体文本,但我也想知道为什么结果不一样


祝你调查顺利

我总是使用两种字体,一种是普通字体,一种是粗体字体。所以当你需要加粗时,只需改变你的字体。从未遇到过问题,您也可以创建自定义textviewplus

大概是这样的:

/res/values/attrs.xml

<resources>   
<attr name="fontFamily" format="enum">
    <enum name="helvetica" value="0"/>
    <enum name="helvetica_bold" value="1"/>
</attr>
}

如何使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
        <com.myapp.viewhelpers.TextViewPlus 
        app:fontFamily="helvetica"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fontFamily="helvetica"
        android:text="helo world"/>


你试过了吗?@jenzz我在发帖之前试过了。这没什么区别。据我所知,问题来自于我在标题上设置的
字体.BOLD
,我在零件。正文看起来不错。谢谢你的回复。我很快会回复你的。它在API 8和10上确实失败了。我将在其他api级别上尝试这种方法,但到目前为止,在17、18、19上使用bold的效果与预期一致。以下是您发布的测试类的输出:。再次感谢您的投入+1很高兴知道,所以如果谷歌的编程设置Bold do的方法失败了,我认为这肯定是一个bug。我不知道他们会不会把它修好。。。我想谷歌的人现在只想听kitkat!嗯,我一直在使用这种方法,只要我可以使用粗体和常规字体。但是,在这种特殊情况下,我只能访问常规版本。看来唯一的办法是购买字体的粗体版本并使用您的解决方案。谢谢你抽出时间。
public class TextViewPlus extends TextView{

private static final String TAG = "TextViewPlus";

public TextViewPlus(Context context) {
    super(context);
}

public TextViewPlus(Context context, AttributeSet attrs) {
    super(context, attrs);
    setCustomFont(context, attrs);
}

public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setCustomFont(context, attrs);
}

private void setCustomFont(Context ctx, AttributeSet attrs) {
    TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
    int customFont = a.getInt(R.styleable.TextViewPlus_fontFamily, -1);
    setCustomFont(ctx, customFont);
    a.recycle();
}

public boolean setCustomFont(Context ctx, int font) {
    Typeface tf = null;
    try {
    tf = Typefaces.get(ctx, font);  
    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }

    setTypeface(tf);  
    return true;
}

public boolean setCustomFont(Context ctx, String asset) {
    Typeface tf = null;
    try {
    tf = Typefaces.get(ctx, asset);  
    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }

    setTypeface(tf);
    return true;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.myapp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
        <com.myapp.viewhelpers.TextViewPlus 
        app:fontFamily="helvetica"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fontFamily="helvetica"
        android:text="helo world"/>