[英]Android: Navigation Drawer Icons
I am using template from AndroidHive: Material Design and decide to add some icon on each navigation drawer row. 我正在使用AndroidHive:Material Design中的模板,并决定在每个导航抽屉行上添加一些图标。 I already modified the model NavDrawerItem, getter and setter for icon was added. 我已经修改了模型NavDrawerItem,为图标添加了getter和setter。
// Constructor
public NavDrawerItem(boolean showNotify, String title, int icon) {
this.showNotify = showNotify;
this.title = title;
this.icon = icon;
}
public String getTitle() {
return title;
}
public int getIcon(){
return icon;
}
public void setTitle(String title) {
this.title = title;
}
public void setIcon(int icon){
this.icon = icon;
}
Also its adapter (NavigationDrawerAdapter) was modified, adding icon support 还对其适配器(NavigationDrawerAdapter)进行了修改,增加了对图标的支持
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
holder.icon.setImageResource(current.getIcon());
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon_drawer);
}
}
I know this drawer was added on onCreate method of FragmentDrawer.java 我知道这个抽屉是在FragmentDrawer.java的onCreate方法上添加的
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// drawer labels
titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
icons = getActivity().getResources().getIntArray(R.array.nav_drawer_icon);
}
This is my array from string.xml 这是我来自string.xml的数组
<string-array name="nav_drawer_labels">
<item>@string/nav_item_home</item>
<item>@string/nav_item_data_visualization</item>
<item>@string/nav_item_pending_item</item>
<item>@string/nav_item_my_profile</item>
<item>@string/nav_item_about_us</item>
</string-array>
<string-array name="nav_drawer_icon">
<item>@drawable/ic_home</item>
<item>@drawable/ic_data_visualization</item>
<item>@drawable/ic_pending_item</item>
<item>@drawable/ic_profile_drawer</item>
<item>@drawable/ic_about</item>
</string-array>
And my navigation drawer item was populated from resource using this piece of code in FragmentDrawer.java 我的导航抽屉项是使用FragmentDrawer.java中的这段代码从资源中填充的
public static List<NavDrawerItem> getData() {
List<NavDrawerItem> data = new ArrayList<>();
// preparing navigation drawer items
for (int i = 0; i < titles.length; i++) {
NavDrawerItem navItem = new NavDrawerItem();
navItem.setTitle(titles[i]);
System.out.println(titles[i]);
navItem.setIcon(icons[i]);
System.out.println(icons[i]);
data.add(navItem);
}
return data;
}
My problem is populating icon from resource , in the code above System.out.println(icons[i]); 我的问题是在System.out.println(icons [i]);上面的代码中从resource填充图标 。 always give me 0 value. 总是给我0值。 I think thats why I can't set imageResource of my navigation drawer icon. 我认为这就是为什么我无法设置导航抽屉图标的imageResource的原因。
Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。
Problem is in line: 问题所在:
icons = getActivity().getResources().getIntArray(R.array.nav_drawer_icon);
To read resources id's from array you can use this code: 要从数组读取资源ID,可以使用以下代码:
icons = new int[titles.length];
TypedArray iconsArray = getResources().obtainTypedArray(R.array.nav_drawer_icon);
for (int i = 0; i < icons.length; i++)
icons[i] = iconsArray.getResourceId(i, -1);
iconsArray.recycle();
But probably the best action will be to use NavigationView . 但是,最好的操作可能是使用NavigationView 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.