如何设置动态声明的ImageButtons的ImageResource?

MRe*_*aat 0 android imagebutton

我有一些动态声明ImageButtons,这些ImageButtons没有'并且它们在a中声明LinearLayout,我想创建一个方法来ImageButtons在调用时更改这些的图像资源,因为我没有为这些ImageButtons我预定义的ID '中号使用功能getChildAt()LinearLayout,但getChildAt()不提供setImageResource()的,它只是提供setBackgroundResource()这个功能不进行替换的旧形象,也并不适合在旧图片,因为它提供了一个背景不是图像资源,是什么我能做到吗?

这是我的方法代码:

private void my_method(int drawable) {

        int count = Righter.getChildCount(); // Righter is the LinearLayout
        for (int i = 1; i < count; i++) {
            Righter.getChildAt(i).setBackgroundResource(drawable); // here is where i change the background image 
            Righter.getChildAt(i).setClickable(true);
        }

    }
Run Code Online (Sandbox Code Playgroud)

Gop*_*opi 6

getChildAt()回报View.你需要强制转换这一ViewImageButton,并调用setImageResource()方法...

    ImageButton imageButton = (ImageButton) linearLayout.getChildAt(0);
    imageButton.setImageResource(R.drawable.btnimage1);
Run Code Online (Sandbox Code Playgroud)