3

我正在为古吉拉特语开发新闻应用程序。现在我的问题是它运行良好,但它显示 Squares([]) 而不是字体,所以我怎样才能使它在古吉拉特语中可见,以便它可以显示古吉拉特语字体。

先感谢您。

4

2 回答 2

4

首先复制资产文件夹中的字体,然后写入

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

接着 :

text.setTypeFace(tf);
于 2012-03-29T11:02:47.340 回答
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 类...如下 public class TypefacedTextView extends 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 是类名。

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

够了……

于 2012-03-29T13:18:23.473 回答