2014-04-10 79 views
0

我需要一种使用字符串而不是“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(); 

}

回答

0

你只是在寻找:

getString(R.string.name); 

您需要一个上下文。在片段或活动,您只需调用的getString但如果不工作无论出于何种原因,它只是一个快捷方式到

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

他说,他的代码是在活动,这样他就可以只使用'的getString()' –

+0

他确实谢谢。有趣的是,getString只是另一个调用的快捷方式。很难将很多有趣的信息包含到关于getString()的问题中,所以我将离开它 – dabluck

+0

谢谢你完美的工作! – Hastalafiesta

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) 
....