1. 程式人生 > >用java創建imageView組件

用java創建imageView組件

事件 gen log over style ger source near method

        setContentView(R.layout.activity_main);
        //獲得id為ll的線性布局
        LinearLayout layout=(LinearLayout)this.findViewById(R.id.ll);
        //創建imageView的組件
        final ImageView imge=new ImageView(MainActivity.this);
        imge.setImageResource(images[currentImage]);
        //將imageView組件添加到id=ll的線性布局裏去
layout.addView(imge); //註冊事件監聽器 imge.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } });

ps

之前定義了圖片數組

int []images=new
int[] { R.drawable.list1, R.drawable.list2, R.drawable.list3, R.drawable.list4, R.drawable.list5, R.drawable.list6, R.drawable.list7, R.drawable.list8, }; int currentImage=0;

用java創建imageView組件