动态图标到导航中的菜单项在Android中查看

时间:2015-12-10 15:05:38

标签: android image menu navigation-drawer navigationview

我的应用程序中的support.design.widget.NavigationView。我可以动态地将菜单项(图像+文本)添加到导航视图。使用SetIcon API从drawable添加图像。但我现在的问题是需要添加从服务器动态获取的图像。我浏览谷歌,但没有找到任何解决方案如何做到这一点。 SetIcon将只将drawable或resource id作为Arguments。请告诉我如何实现这一目标。

2 个答案:

答案 0 :(得分:1)

为了将从API调用中检索到的图像添加到#### import the simple module from the paraview from paraview.simple import * #### disable automatic camera reset on 'Show' paraview.simple._DisableFirstRenderCameraReset() # create a new 'ExodusIIReader' canex2 = ExodusIIReader(FileName=['/Users/kmorel/data/ParaViewDataNew/can.ex2']) canex2.ElementVariables = ['EQPS'] canex2.PointVariables = ['DISPL', 'VEL', 'ACCL'] canex2.GlobalVariables = ['KE', 'XMOM', 'YMOM', 'ZMOM', 'NSTEPS', 'TMSTEP'] canex2.NodeSetArrayStatus = [] canex2.SideSetArrayStatus = [] canex2.ElementBlocks = ['Unnamed block ID: 1 Type: HEX', 'Unnamed block ID: 2 Type: HEX'] canex2.ApplyDisplacements = 0 # create a new 'Temporal Statistics' temporalStatistics1 = TemporalStatistics(Input=canex2) # create a new 'Plot Over Line' plotOverLine1 = PlotOverLine(Input=temporalStatistics1, Source='High Resolution Line Source') # init the 'High Resolution Line Source' selected for 'Source' plotOverLine1.Source.Point1 = [-7.878461837768555, 0.0, -14.999999046325684] plotOverLine1.Source.Point2 = [8.312582015991211, 8.0, 4.778104782104492] # Properties modified on plotOverLine1 plotOverLine1.Tolerance = 2.22044604925031e-16 # Properties modified on plotOverLine1.Source plotOverLine1.Source.Point1 = [0.0, 5.0, -15.0] plotOverLine1.Source.Point2 = [0.0, 5.0, 0.0] # save data SaveData('plot_over_line.csv', proxy=plotOverLine1) ,您必须将该图像转换为NavigationView(您回答了自己的问题!):)。

要执行此操作,您首先必须将Drawable转换为ByteArray,如下所示:

Bitmap

然后您可以将Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length); 转换为Bitmap,如此:

Drawable

获得Drawable d = new BitmapDrawable(getResources(), bitmap); 后,您可以像这样更新Drawable

Menu

或者,如果您愿意,请在menu.getItem(0).setIcon(yourNewDrawable); 初始化时保留对MenuItem变量的引用,并在您认为合适时更改它(例如,当您的API调用完成时!)。

希望这有帮助。

答案 1 :(得分:0)

首先从NavigationView中获取菜单:

Menu menu = mNavigationView.getMenu();

然后将您的项目添加到菜单,请记住获取返回的MenuItem,以便稍后可以添加图标:

MenuItem item = menu.add(groupId, menuItemId, Order, "Menu Item 1 Title");
item.setIcon(R.drawable.ic_some_menu_item_icon); // add icon with drawable resource