Android中setImageResource()的目的是什么?

时间:2017-06-15 12:21:13

标签: android

如果图像已经在Xml中定义,并且当我运行应用程序时,它会在模拟器的屏幕上弹出而没有任何问题,Java代码中setImageResource()方法的目的是什么?< / p>

2 个答案:

答案 0 :(得分:0)

我们可以使用 setImageResource()方法以编程方式将图像资源设置为 ImageView : 例如:

 ImageView img = (ImageView)findViewById(R.id.img);
 // supossing to have an image called ic_play inside my drawables.
 myImageView.setImageResource(R.drawable.ic_play);

答案 1 :(得分:0)

对于任何动态编程,您可能需要在运行时更改ImageView的内容,并根据您可以通过Web服务获得的资源来执行此操作

public static IForm<StandardInfoForm> BuildForm()
        {
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously

            var parser = new Parser();
            return new FormBuilder<StandardInfoForm>()
                .Message("Welcome to reporting information!!")
                .Field(new FieldReflector<StandardInfoForm>( nameof(Report))
                    .SetActive( state => state.AskToChooseReport)
                    .SetNext( (value, state) =>
                    {
                        var selection = (ReportType)value;
                        if (selection == ReportType.Application)
                        {
                            state.ReportRequest = "application";
                            return new NextStep(new[] { nameof(ReportApplicationName) });
                        }
                        else if (selection == ReportType.Emotion)
                        {
                            state.ReportRequest = "emotion";
                            return new NextStep(new[] { nameof (EmotionName) });
                        }
                        else if (selection == ReportType.AppVsEmotion)
                        {
                            state.ReportRequest = "application,emotion";
                            return new NextStep(new[] { nameof (ReportApplicationName), nameof(EmotionName) });
                        }
                        else if (selection == ReportType.Help)
                        {
                            state.ReportRequest = "help";
                            return new NextStep(new[] { nameof(ReportApplicationName) });
                        }
                        else
                        {
                            return new NextStep();
                        }
                    }))               
                .Field(nameof(StartDate))
                .Field(nameof(EndDate), EndReportDateActive)                              
                .Confirm("Would you like to confirm.Yes or No")
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
                .Build();

        }