2012-08-09 90 views
0

我在英文語言中創建了一個應用程序。現在我改變阿拉伯語語言的幫助下的res/values-cn它的工作正常。現在我想在我的應用程序中實現菜單。我已按照此鏈接製作自定義菜單。 http://www.codeproject.com/Articles/173121/Android-Menus-My-Way#如何在Android中將MenuItems的語言更改爲阿拉伯語?

它的工作正常。此外,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 ([email protected]) 
* @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> 
+0

你是指第二張截圖中「第一張」,「第二張」等標題?你想用阿拉維語來看他們嗎?使用strings.xml並提供你想要什麼語言的標題 – dilix 2012-08-09 12:38:42

+0

我試過了。它在模擬器中工作正常。但在設備中沒有顯示阿拉伯語,取而代之的是小盒子顯示爲未知語言。 – 2012-08-09 12:40:09

+0

也許你的自定義佈局錯了 - 你能展示它嗎? – dilix 2012-08-09 12:42:11

回答

0

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

我已經提取了你提供的樣本,將它導入到我的eclipse中,從文件夾值中的eclipse strings.xml打開,使用「ثان」進行多個字段測試,並添加項目時添加getString代碼。

我得到什麼:

enter image description here

我覺得有一些麻煩與字符集時,您已經創建了新的string.xml - 檢查該文件中獲取UTF8(因爲它它可以在Windows中工作(cp1251和不工作在android(linux == utf8)))。

+0

是的,這也是我的朋友。它也不起作用。請建議其他幫助 – 2012-08-09 12:42:18

+0

PLZ查看我的編輯 – 2012-08-09 12:46:49

+0

檢查更新的答案 – dilix 2012-08-09 12:56:01

相關問題