我可以更改Android应用程序的语言吗?

时间:2012-03-29 10:57:06

标签: android android-fonts

我正在开发古吉拉特语的新闻申请。 现在我的问题是它运行良好,但它显示Squares([])而不是字体,所以我如何使它在古吉拉特语中可见,以便它可以显示古吉拉特语字体。

提前谢谢。

2 个答案:

答案 0 :(得分:4)

首先复制assets文件夹中的字体,然后编写

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/gujaratifont.otf");

然后:

text.setTypeFace(tf);

答案 1 :(得分:0)

这是使用xml更改字体字体的方法。

  1. 定义一个新的NameSpace:xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"(“com.nound.test”是Manifest.xml中定义的包名称)

  2. 在/res/values/attrs.xml文件中定义自定义属性。

    <resources>
        <declare-styleable name="TypefacedTextView">
             <attr name="textStyle" format="string"/>
        </declare-styleable> </resources>
    
  3. 扩展TextView的Java类...如下所示 公共类TypefacedTextView扩展TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        //Typeface.createFromAsset doesn't work in the layout editor. Skipping...
        if (isInEditMode()) {
            return;
        }
    
        TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView);
        //String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface);
        String fontNameForBold = styledAttrs.getString(R.styleable.TypefacedTextView_textStyle);
        styledAttrs.recycle();
    
        if (fontNameForBold!=null && fontNameForBold.equals("bold")) {
            Typeface typeface_bold = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma_bold.ttf");
            setTypeface(typeface_bold);
        }else{
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/tahoma/tahoma.ttf");
            setTypeface(typeface);
        }
    }
    
  4. 这里是您想要更改字体字体的布局代码。在这个“com.nound.test.ui”中是上面的(3点)java文件包名。 TypefacedTextView是类名。

  5. <com.nound.test.ui.TypefacedTextView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Custom fonts in XML are easy_bold font"
                your_namespace:textStyle="bold" />
    
    
    <com.nound.test.ui.TypefacedTextView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:your_namespace="http://schemas.android.com/apk/res/com.nound.test"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Custom fonts in XML are easy_regular font"
                />
    

    足以做......

相关问题