1

英語でアプリケーションを作成しました。今、私はres/values-cnの助けを借りてそれをアラビア語に変更しています。ここで、アプリケーションにメニューを実装したいと考えています。このリンクをたどってカスタムメニューを作成しました。http://www.codeproject.com/Articles/173121/Android-Menus-My-Way# .

その正常に動作します。また、MenuItem のキャプションもエミュレーターでアラビア語に変更されます。しかし、モバイルで解決策を見つけることができません。

私の質問は、Android デバイスで MenuItem の Caption を変更する方法です。

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>
4

1 に答える 1

0

cmi.setCaption( getString (R.string.youString));を使用します。

あなたが提供したサンプルを取得し、それを私のEclipseにインポートし、フォルダの値にあったEclipseのstrings.xmlから開き、「ثان」でもう1つのフィールドテストを行い、アイテムを追加するときにgetStringコードを追加します。

私が得たもの:

ここに画像の説明を入力

新しいstring.xmlを作成したときに、charsetに問題があると思います-このファイルがutf8を取得するかどうかを確認してください(Windowsでは動作するため(cp1251で、androidでは動作しません)(linux == utf8)) )。

于 2012-08-09T12:40:51.280 に答える