Android 如何使用for循环设置imagebutton资源
如何使用for循环为一系列imagebutton设置相同的图像资源Android 如何使用for循环设置imagebutton资源,android,for-loop,imagebutton,Android,For Loop,Imagebutton,如何使用for循环为一系列imagebutton设置相同的图像资源 ImageButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30; public void setresource() { for (int i = 0
ImageButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14,
b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27,
b28, b29, b30;
public void setresource() {
for (int i = 0 ; i <= 15; i++){
b[i].setImageResource(R.drawable.playzz);
}
}
ImageButton b1、b2、b3、b4、b5、b6、b7、b8、b9、b10、b11、b12、b13、b14、,
b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,
b28、b29、b30;
public void setresource(){
对于(int i=0;i请尝试以下操作。您尚未初始化ImageButton
ImageButton b[];
在onnCreate(param)中
b=新建图像按钮[15];
对于(int i=0;我有u初始化ImageButton@Segi表达式的类型必须是数组类型,但解析为int
b = new ImageButton[15];
for (int i = 0 ; i <15; i++){
b[i] = new ImageButton(ActiivtyName.this);
b[i].setImageResource(R.drawable.playzz);
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ll"
tools:context=".MainActivity" >
</LinearLayout>
</ScrollView>
public class MainActivity extends Activity {
ImageButton b[];
LinearLayout ll;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);// initialize linearlayout
setresource();
}
public void setresource()
{
b = new ImageButton[15];
for (int i = 0 ; i < 15; i++){
b[i]= new ImageButton(MainActivity.this); // initilize
b[i].setMaxWidth(40); // set the maxwidth
b[i].setImageResource(R.drawable.ic_launcher); // set background image
ll.addView(b[i]); // add the imagebutton to the linearlayout
}
}
}