我可以帮您翻译成中文:我能改变Android应用的语言吗?

3
我正在开发一款古吉拉特语新闻应用程序。目前遇到的问题是,应用程序运行正常,但显示的是方块([]),而不是字体。请问如何使其在古吉拉特语中可见,以便显示古吉拉特语字体?
提前感谢您的帮助。

你有古吉拉特语的字体文件吗? - Akhil
2个回答

4

首先复制字体到资产文件夹中,然后编写:

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

然后:
text.setTypeFace(tf);

1
是的,otf和ttf都可以使用。 - Bhavin
我尝试了这个,但它没有给出正确的输出,仍然显示方块。 - js salat
insertData("3","hello.jpg","'સુપર ફાઇટ લીગ' ના આગામી ઉદ્ઘાટન સમારોહમાં 'અનારકલી' ધમાલ મચાવશે ","這是第三個項目");TextView cap =(TextView)view.findViewById(R.id.caption); Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/gujaratifont.ttf"); cap.setTypeface(tf); - js salat
如果这不起作用,请将其保存在String.xml文件中,例如:<string name="guj">'સુપર ફાઇટ લીગ' ના આગામી ઉદ્ઘાટન સમારોહમાં 'અનારકલી' ધમાલ મચાવશે</string>。 - Bhavin

0

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

  1. 定义一个新的命名空间: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. Java类继承TextView...如下所示 public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        //Typeface.createFromAsset在布局编辑器中无法使用。跳过...
        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"
            />

只需要做...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接