如何将android中的菜单项的语言改为阿拉伯语?

时间:2012-08-09 12:24:53

标签: android

我用英语语言创建了一个应用程序。现在我在 res / values-cn 的帮助下用阿拉伯语语言改变它,并且它的工作正常。现在我想在我的应用程序中实现菜单。我已按照此链接制作自定义菜单。 http://www.codeproject.com/Articles/173121/Android-Menus-My-Way#

工作正常。此外,MenuItem的标题也会在模拟器中更改为阿拉伯语。但无法在手机中找到解决方案。

我的问题是,如何更改Android设备中MenuItem的标题。

This is my code which i edited in Demo.java

package com.menu;

import java.util.ArrayList;
import java.util.Locale;

import com.menu.CustomMenu.OnMenuItemSelectedListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * Demo class
 *
 * This is class demonstrates how to use the custom menu helper classes.
 *
 * @category   Demos
 * @author     William J Francis (w.j.francis@tx.rr.com)
 * @copyright  Enjoy!
 * @version    1.0
 */

public class Demo extends Activity implements OnMenuItemSelectedListener {

    /**
     * Some global variables.
     */
    Typeface typeface;
    private CustomMenu mMenu;
    public static final int MENU_ITEM_1 = 1;
    public static final int MENU_ITEM_2 = 2;
    public static final int MENU_ITEM_3 = 3;
    public static final int MENU_ITEM_4 = 4;

    /**
     * Always called when an Android activity launches.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //create the view
        super.onCreate(savedInstanceState);
        Locale locale = new Locale("cn");
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale; 
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        typeface = Typeface.createFromAsset(getAssets(),"arabtype.ttf");
        setContentView(R.layout.main);
        //initialize the menu
        mMenu = new CustomMenu(this, this, getLayoutInflater());
        mMenu.setHideOnSelect(true);
        mMenu.setItemsPerLineInPortraitOrientation(4);
        mMenu.setItemsPerLineInLandscapeOrientation(8);
        //load the menu items
        loadMenuItems();
    }

    /**
     * Snarf the menu key.
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            doMenu();
            return true; //always eat it!
        }
        return super.onKeyDown(keyCode, event); 
    } 

    /**
     * Load up our menu.
     */
    private void loadMenuItems() {
        //This is kind of a tedious way to load up the menu items.
        //Am sure there is room for improvement.


        ArrayList<CustomMenuItem> menuItems = new ArrayList<CustomMenuItem>();
        CustomMenuItem cmi = new CustomMenuItem();
        //String str = "hello";
        cmi.setTypeface(typeface);
        cmi.setCaption(getString(R.string.first)); 
        cmi.setImageResourceId(R.drawable.icon1);
        cmi.setId(MENU_ITEM_1);
        menuItems.add(cmi);
        cmi = new CustomMenuItem();
        cmi.setCaption(getString(R.string.second));
        cmi.setImageResourceId(R.drawable.icon2);
        cmi.setId(MENU_ITEM_2);
        menuItems.add(cmi);
        cmi = new CustomMenuItem();
        cmi.setCaption("Third");
        cmi.setImageResourceId(R.drawable.icon3);
        cmi.setId(MENU_ITEM_3);
        menuItems.add(cmi);
        cmi = new CustomMenuItem();
        cmi.setCaption("Fourth");
        cmi.setImageResourceId(R.drawable.icon4);
        cmi.setId(MENU_ITEM_4);
        menuItems.add(cmi);
        if (!mMenu.isShowing())
        try {
            mMenu.setMenuItems(menuItems);
        } catch (Exception e) {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Egads!");
            alert.setMessage(e.getMessage());
            alert.show();
        }
    }

    /**
     * Toggle our menu on user pressing the menu key.
     */
    private void doMenu() {
        if (mMenu.isShowing()) {
            mMenu.hide();
        } else {
            //Note it doesn't matter what widget you send the menu as long as it gets view.
            mMenu.show(findViewById(R.id.any_old_widget));
        }
    }

    /**
     * For the demo just toast the item selected.
     */
    @Override
    public void MenuItemSelectedEvent(CustomMenuItem selection) {
        Toast t = Toast.makeText(this, "You selected item #"+Integer.toString(selection.getId()), Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
    }
}

和strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Demo!</string>
    <string name="app_name">CustomMenu</string>
    <string name="first">الأول</string>
    <string name="second">ثان</string>
</resources>

1 个答案:

答案 0 :(得分:0)

使用cmi.setCaption(getString(R.string.youString));

我已经提供了您提供的样本,将其导入我的eclipse,从文件夹值中的eclipse strings.xml打开,再使用“ثان”进行一次字段测试,并在添加项目时添加getString代码。

我得到了什么:

enter image description here

我认为当你创建新的string.xml时charset会有一些麻烦 - 检查这个文件是否得到utf8(因为它可以在windows中工作(cp1251并且在android中不起作用(linux == UTF8)))。

相关问题