0

我有一个预装了一些内容的文本视图。我想要的是用英文显示某些内容的某些部分,用中文显示一些内容。比如说我有三个英文段落,那么每个这样的段落都应该跟中文段落。由于长度不同,我不能使用内容的跨越。请为此提供解决方案或更好的替代方案。

谢谢 :)

4

2 回答 2

1

HTML你可以像这样 格式化它:

MyTypeFace.class

package my.app;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class MyTypeFace extends TypefaceSpan {
private final Typeface newType;
public MyTypeFace(String family, Typeface type) {
    super(family);
    newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }
    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }
    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }
    paint.setTypeface(tf);
}
}  

现在,继续从 中获取故事,String.xml在它们上应用字体,然后显示它们。

String text1=findViewById(R.string.text1);  
String text2=findViewById(R.string.text2);  

TextView textView = (TextView) findViewById(R.id.custom_fonts);  
txt.setTextSize(30);
Typeface font1 = Typeface.createFromAsset(getAssets(), "english.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "chinese.ttf");   
text1.setSpan (new MyTypeFace("", font1), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
text2.setSpan (new MyTypeFace("", font2), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
String totalText=text1+"<br>"+text2;  
textView.setText(Html.fromHtml(totalText));
于 2012-08-23T11:49:09.213 回答
0

您可以尝试从此示例中找出答案:

TextView text = new TextView(context);
text.setText(Html.fromHtml("<b>" + "some text" + "</b>" +  "<br />" + 
            "<small>" + "some text" + "</small>" + "<br />" + 
            "<small>" + "some text" + "</small>"));
于 2012-08-23T11:47:58.273 回答