简体   繁体   English

如何使用背后的代码在LinearLayout中添加多个ImageView

[英]How to add multiple ImageView's inside of LinearLayout using code behind

在此处输入图片说明

I need to create something like that, a Container with 8 ImageView's and be able to get each of then, I already have the code to take the image at the gallery, but I'm stuck on that, my current code I have this: 我需要创建一个类似的容器,带有8个ImageView的容器,并且能够获取每个容器,我已经有了在图库中拍摄图像的代码,但是我仍然坚持这一点,我目前的代码是这样的:

<LinearLayout
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <HorizontalScrollView
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/linearImages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </LinearLayout>
    </HorizontalScrollView>

Activity : 活动内容:

linearImages = (LinearLayout)findViewById(R.id.linearImages);
Test = new ImageView(this);
galleryIntent(){.......
Test.setImageBitmap(bitmap);
linearImages.addView(Test);

But only works with two Images! 但是仅适用于两个图像!

HorizontalScrollView更改为ScrollView

Try defining LayoutParams : 尝试定义LayoutParams

Image.setLayoutParams(newLinearLayout.LayoutParams(
                     LinearLayout.LayoutParams.MATCH_PARENT,
                     LinearLayout.LayoutParams.MATCH_PARENT));

then, add your views. 然后,添加您的视图。

LayoutInflater  layoutInflater = (LayoutInflater) 
                         context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(R.layout.control, null);
yourLinerarLayout.add(view); 

Here, R.layout.control is xml file in your layout folder that contain view of single piece of view add whatever time you want. 在这里, R.layout.control是布局文件夹中的xml文件,其中包含单个视图的视图,并且可以根据需要添加任何时间。

try this: 尝试这个:

in your layout: 在您的布局中:

     <HorizontalScrollView
                android:id="@+id/image_scroll"
                android:layout_width="match_parent"
                android:layout_height="300dp" >

                <LinearLayout
                    android:id="@+id/gallery_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" >

                </LinearLayout>
   </HorizontalScrollView>

now for adding use: 现在可以添加使用:

  for (final GalleryItem item : items) {
    ImageView iv = new ImageView(context);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT
                                            , LayoutParams.WRAP_CONTENT);

     param.setMargins(0, 0, 10, 0);
     iv.setLayoutParams(param);
     iv.setImageBitmap(item);
     iv.setAdjustViewBounds(true);
     layout.add(iv);   //your gallery layout
}

Use TableRow and take array of ImageViews . 使用TableRow并获取ImageViews数组。 Pass this ImageViews in to TableView , then pass final TableView in to LinearLayout . 将此ImageViews传递给TableView ,然后将最终的TableView传递给LinearLayout

Some thing like this: 像这样的东西:

 TableRow row = new TableRow(mContext);
 final ImageView[] myTextViews = new ImageView[n];

Also, to get call back for each ImageView use setTag and getTag functions. 另外,要获取每个ImageView回调,请使用setTaggetTag函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM