Android 如何在安卓2.2中设置malayalam字体(印度本地语言)的textview

Android 如何在安卓2.2中设置malayalam字体(印度本地语言)的textview,android,text,Android,Text,我需要用马来文字体显示TextView,马来文字体是印度当地语言。为此,我尝试下面的代码 Typeface custom_typerface = Typeface.createFromAsset(getAssets(), "fonts/AnjaliNewLipi.ttf"); TextView mytext = (TextView)findViewById(R.id.mytext); mytext.setTypeface(custom_typerf

我需要用马来文字体显示
TextView
,马来文字体是印度当地语言。为此,我尝试下面的代码

        Typeface custom_typerface = Typeface.createFromAsset(getAssets(), "fonts/AnjaliNewLipi.ttf");
        TextView mytext = (TextView)findViewById(R.id.mytext);
        mytext.setTypeface(custom_typerface);
        mytext.setText("മലയാള ഭാഷ തന്‍ ഭാവ ശുദ്ധി...");

这在4.2设备上正确显示,但并非在所有设备上都正确显示。如何为所有android设备渲染它?任何帮助都将不胜感激

经过长时间的搜索,我终于找到了答案。。 我将Unicode字体转换为Ascii,用Ascii替换每个Unicode字符。 这是神奇的密码

mytext.replaceAll("oldChar", "newChar");

将此代码用于每个字符。:)

将自定义字体设置为文本视图。

您有两种方式,第一种是第一种方式:

1。将字体文件添加到“资源”文件夹,如果不存在“资源”文件夹,则创建其字体文件

2.创建CustomTypeface类,例如:

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;

public class CustomTypeface {

    private static Typeface typefaseArialCondIt;
    private static Typeface typefaseArialBold;
    private static Typeface typefaseArialRegular;

    public CustomTypeface() {
    }

    public CustomTypeface(Context context) {
        AssetManager assets = context.getAssets();
        CustomTypeface.typefaseArialCondIt = Typeface.createFromAsset(assets, "fonts/ArialCondIt.otf");
        CustomTypeface.typefaseArialBold = Typeface.createFromAsset(assets, "fonts/ArialBold.otf");
        CustomTypeface.typefaseArialRegular = Typeface.createFromAsset(assets, "fonts/ArialRegular.otf");
    }

    public static Typeface gettypefaseArialCondIt() {
        return typefaseArialCondIt;
    }

    public static Typeface settypefaseArialCondIt(Typeface typefaseArialCondIt) {
        return CustomTypeface.typefaseArialCondIt = typefaseArialCondIt;;
    }

    public static Typeface gettypefaseArialBold() {
        return typefaseArialBold;
    }

    public static Typeface settypefaseArialBold(Typeface typefaseArialBold) {
        return CustomTypeface.typefaseArialBold = typefaseArialBold;;
    }

    public static Typeface gettypefaseArialRegular() {
        return typefaseArialRegular;
    }

    public static void settypefaseArialRegular(Typeface typefaseArialRegular) {
        CustomTypeface.typefaseArialRegular = typefaseArialRegular;
    }
}
3.并将自定义字体设置为文本视图:

TextView yourTextView = (TextView) findViewById(R.id.textView1);
yourTextView.setTypeface(CustomTypeface.gettypefaseArialRegular);
或者第二种方法,使用您的字体创建自定义文本视图:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class ArialTextView extends TextView {

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

    @Override
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialBold.otf"));
        } else if (style == Typeface.NORMAL) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialRegular.otf"));
        } else if (style == Typeface.ITALIC) {
            setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialCondIt.otf"));
        }
    }
}
在布局xml中,您可以创建自定义文本视图,例如:

<com.your.package.ArialTextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:text="TextView with your custom font"/>

以上代码正确,应显示马来文字体。但有一个陷阱。如果你的手机不支持这种字体,它不会显示马来语,但会显示一些方框

如果您使用任何文件浏览器并查看system/system/fonts,您将看到设备支持的所有字体

若你们想添加新的字体,你们也可以这样做,但设备必须是根的。
许多便宜的手机只支持很少的字体,但昂贵的手机支持很多字体。

我不确定这是否适用于您的情况,但我使用的是旁遮普字体。我所做的不是复制文本或将文本输入字符串,然后使用字体。您当然会使用字体,但为了使其正常工作,您需要以音译形式输入马来语文本,即如以下印地语示例所示,因为我不懂马来语

普尔

这将以正确的格式显示文本,没有任何问题,但是如果您只需输入

mytext.setText("फूल");  
它很可能无法正确呈现字体。
我希望这有帮助。:)

您不能在string.xml中定义此字符串值并获取此值.mytext.setText(@string/value);这不会有什么区别:(有什么完美的解决方案吗?需要帮助吗?哪些设备不工作?是否有一个共同的线程,比如旧的Android版本?@StileScriss在真实设备HTC Wildfire(Android版本2.2)、HTC One V(安卓4.0)上进行了测试)没有成功。这是一个非常奇怪的答案,因为ASCII不包含印度字符。@Sulthan,但我们可以用这个简单的代码替换任何字符。:)Unicode包含100000多个字符,ASCII包含256个字符。如何将Unicode转换为ASCII?请扩展你的问题,说明什么是不可行的,并解释你到底做了什么才能让它起作用。此问题/答案对未来的访问者没有帮助。请使用您自己的字体,并将其中的每个unicode字符转换为ascii。例如,在我的例子中,
my_text.toLowerCase().replaceAll(“അ", "A)全部替换ആ","B“
,对所有字符使用类似的代码…它是否涉及编辑字体(
.ttf
)文件?如果是,那么这不是一个好的答案,因为它需要专门的工具。如果您的android是AnkiDroid,请查看以下链接:
mytext.setText("फूल");