【问题标题】:How to set textview in malayalam font(An Indian local language) in android 2.2如何在 android 2.2 中以马拉雅拉姆字体(印度当地语言)设置 textview
【发布时间】:2013-08-22 10:49:05
【问题描述】:

我需要以印度当地语言的马拉雅拉姆字体显示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 设备渲染它?任何帮助将不胜感激。

【问题讨论】:

  • 能不能不要在string.xml中定义这个字符串值并取这个值.mytext.setText(@string/value);
  • 不会有区别的..:(
  • 有完美的解决方案吗?需要帮助...
  • 它不能在哪些设备上运行?是否有一个共同的线程,比如旧的 Android 版本?
  • @StilesCrisis 在真机上测试 HTC Wildfire (Android 2.2) , HTC One V (Android 4.0) 没有成功。

标签: android text


【解决方案1】:

上面的代码是正确的,它应该显示马拉雅拉姆字体。但是有一个问题!如果您的手机不支持该字体,则不会显示马拉雅拉姆语,而是会显示一些框。

如果您使用任何文件浏览器并查看系统/系统/字体 - 您将看到设备支持的所有字体。

如果您想添加新字体,您也可以这样做,但设备必须为此植根。 许多便宜的手机只支持几种字体,但昂贵的手机却支持多种字体。

【讨论】:

    【解决方案2】:

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

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

    对每个字符使用此代码。 :)

    【讨论】:

    • 这是一个非常奇怪的答案,因为 ASCII 不包含印度字符。
    • @Sulthan 但是我们可以用这个简单的代码替换任何字符。 :)
    • Unicode 包含超过 100 000 个字符,ASCII 包含 256 个字符。如何将 Unicode 转换为 ASCII?请扩展您的问题以显示未佩戴的内容,并解释您究竟需要做什么才能使其正常工作。此问题/答案对未来的访问者没有帮助。
    • 使用您自己的字体并将其中的每个 unicode 字符转换为 ascii。例如,在我的情况下my_text.toLowerCase().replaceAll("അ", "A").replaceAll("ആ","B"),对所有字符使用这样的代码...
    • 是否涉及编辑字体 (.ttf) 文件?如果是,那么这不是一个好的答案,因为它需要专门的工具。
    【解决方案3】:

    将自定义字体设置为 TextView。

    你有两种方式,先看第一种方式:

    1. 将字体文件添加到 assets 文件夹,如果不存在 assets 文件夹,请创建它。

    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:

    TextView yourTextView = (TextView) findViewById(R.id.textView1);
    yourTextView.setTypeface(CustomTypeface.gettypefaseArialRegular);
    

    或者第二种方法,使用您的字体创建自定义 TextView:

    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中创建自定义TextView,例如:

    <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"/>
    

    【讨论】:

    【解决方案4】:

    我不确定这是否适用于您的情况,但我使用的是旁遮普字体。我所做的不是复制或将文本输入到字符串中,然后使用字体。您当然确实使用了该字体,但为了使其正常工作,您需要在音译中输入马拉雅拉姆语文本,即如下面的印地语示例,因为我不知道马拉雅拉姆语。

    PUl

     Typeface custom_typerface = Typeface.createFromAsset(getAssets(), "fonts/AnjaliNewLipi.ttf");
        TextView mytext = (TextView)findViewById(R.id.mytext);
        mytext.setTypeface(custom_typerface);
        mytext.setText("PUl"); //Assuming you know Hindi PUl in hindi is फूल
    

    这将以正确的格式显示文本,但是如果您只是输入

    mytext.setText("फूल");  
    

    它很可能无法正确呈现字体。 我希望这有帮助。 :)

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2017-06-27
      • 2013-10-31
      • 2014-04-25
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多