按字符串更改导航抽屉名称项

时间:2014-04-10 14:25:09

标签: java android android-fragments navigation-drawer

我需要一种方法来使用字符串而不是“PERFORMANCE”,“MALI”...我试图通过“@ string / resource_name”来对它们进行处理但是它不起作用,当我打开它显示的菜单而不是链接名称“@ string / resource_name”。

MainActivity:

 //Show the items into the menu.
@Override
protected NavDrawerActivityConfiguration getNavDrawerConfiguration() {

    NavDrawerItem[] menu = new NavDrawerItem[] {

            //This sets the name, mine is a multilanguage app so I wanna find a way to use @string/resource instead
            //of "PERFORMANCE", ecc.
            NavMenuItem.create(1,"PERFORMANCE", "", false, this),
            NavMenuItem.create(2, "MALI", "", false, this), 
            NavMenuItem.create(3, "BATTERY", "", false, this),
            NavMenuItem.create(4, "CPU", "", false, this), 
            NavMenuItem.create(5, "MISC", "", false, this),
            NavMenuItem.create(6, "NET", "", false, this),
            NavMenuItem.create(7, "HELP", "", false, this),
            NavMenuItem.create(8, "GUIDE", "", false, this)};

    NavDrawerActivityConfiguration navDrawerActivityConfiguration = new NavDrawerActivityConfiguration();
    navDrawerActivityConfiguration.setMainLayout(R.layout.main);
    navDrawerActivityConfiguration.setDrawerLayoutId(R.id.drawer_layout);
    navDrawerActivityConfiguration.setLeftDrawerId(R.id.left_drawer);
    navDrawerActivityConfiguration.setNavItems(menu);
    navDrawerActivityConfiguration.setDrawerShadow(R.drawable.drawer_shadow);       
    navDrawerActivityConfiguration.setDrawerOpenDesc(R.string.drawer_open);
    navDrawerActivityConfiguration.setDrawerCloseDesc(R.string.drawer_close);
    navDrawerActivityConfiguration.setBaseAdapter(
        new NavDrawerAdapter(this, R.layout.navdrawer_item, menu ));
    return navDrawerActivityConfiguration;
}

NavmenuItem:

    public class NavMenuItem implements NavDrawerItem {

public static final int ITEM_TYPE = 1 ;

private int id ;
private String label ;  
private boolean updateActionBarTitle ;

private NavMenuItem() {
}

public static NavMenuItem create( int id, String label, String icon, boolean updateActionBarTitle, Context context ) {
    NavMenuItem item = new NavMenuItem();
    item.setId(id);
    item.setLabel(label);
    item.setUpdateActionBarTitle(updateActionBarTitle);
    return item;
}

@Override
public int getType() {
    return ITEM_TYPE;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}


@Override
public boolean isEnabled() {
    return true;
}

@Override
public boolean updateActionBarTitle() {
    return this.updateActionBarTitle;
}

public void setUpdateActionBarTitle(boolean updateActionBarTitle) {
    this.updateActionBarTitle = updateActionBarTitle;
}

}

NavdrawerItem:

 public interface NavDrawerItem {
public int getId();
public String getLabel();
public int getType();
public boolean isEnabled();
public boolean updateActionBarTitle();

}

2 个答案:

答案 0 :(得分:0)

试试这样:

NavDrawerItem[] menu = new NavDrawerItem[] {

        //This sets the name, mine is a multilanguage app so I wanna find a way to use @string/resource instead
        //of "PERFORMANCE", ecc.
        NavMenuItem.create(1,getString(R.string.performance_name), "", false, this) 
....

答案 1 :(得分:0)

您只是在寻找:

getString(R.string.name);  

您需要一个上下文。在片段或活动中,您可以调用getString,但如果由于某种原因它不起作用,它只是一个快捷方式

context.getResources().getString(R.string.name);