Android Linearlayout中动态添加ImageView

来源:互联网 发布:ice框架 java 编辑:程序博客网 时间:2024/04/28 02:09

需求:界面有时候显示图片的数量未知,需要在代码中动态添加图片。
方法步骤:
1.布局:

<LinearLayout     android:id="@+id/ll_group"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     android:orientation="horizontal"    android:layout_centerHorizontal="true"></LinearLayout>

2.代码:

LinearLayout llGroup = (LinearLayout) findViewById(R.id.ll_group);//size:代码中获取到的图片数量private void addGroupImage(int size){    llGroup.removeAllViews();  //clear linearlayout    for (int i = 0; i < size; i++) {        ImageView imageView = new ImageView(this);        imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  //设置图片宽高        imageView.setImageResource(R.drawable.ic_launcher); //图片资源        llWindLayout.addView(imageView); //动态添加图片    }}
0 0
原创粉丝点击