Android:动态创建的NetworkImageView的宽度和高度返回零

标签 android android-volley

我想创建一个水平图像列表,用户可以动态添加(和删除)图像,除了第一个(它的工作方式类似于添加图像按钮)。用户将单击第一个以在其左侧添加更多图片。如下所示,我创建了一个水平滚动和线性布局,它最初有一个 NetworkImageView,它是图像添加器按钮。

xml 有:

...
        <HorizontalScrollView
            android:id="@+id/horizontal_scroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

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

                <com.android.volley.toolbox.NetworkImageView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/imageViewAdd"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:background="@color/colorAccent"
                    android:contentDescription="@string/description" />


            </LinearLayout>

        </HorizontalScrollView>
...

然后在代码中,当用户点击那个NetworkImageView的时候,我想在它的前面一个一个的添加其他的。

...
// On create code
layout = (LinearLayout) findViewById(R.id.linear);
        imageViewAdd = (NetworkImageView)findViewById(R.id.imageViewAdd);
        ImageLoader imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
                .getImageLoader();
        imageLoader.get(Config.IMAGE_PATH_URL, ImageLoader.getImageListener(imageViewAdd,
                R.drawable.image, android.R.drawable
                        .ic_menu_camera));
        imageViewAdd.setImageUrl(Config.IMAGE_PATH_URL, imageLoader);
        imageViewAdd.setOnClickListener(this);
        totalImageCount = 0;
...
// Create and add new image view
NetworkImageView newView = new NetworkImageView(this);
        newView.setId(id);
        newView.setPadding(2, 2, 2, 2);
    newView.setScaleType(ImageView.ScaleType.FIT_XY);
    layout.addView(newView, id);

    ImageLoader imageLoader = CustomVolleyRequest.getInstance(this.getApplicationContext())
            .getImageLoader();
    imageLoader.get(Config.IMAGE_PATH_URL, ImageLoader.getImageListener(newView,
            R.drawable.image, android.R.drawable
                    .ic_dialog_alert));
    newView.setImageUrl(Config.IMAGE_PATH_URL, imageLoader);
    newView.requestLayout();
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            (int) getResources().getDimension(R.dimen.imageview_height), // 100 dp each
            (int)getResources().getDimension(R.dimen.imageview_width));
    newView.setLayoutParams(layoutParams);
...

在代码的后面,当我真正想为其设置图像路径时,我读取宽度和高度如下:

int targetW = newView.getWidth();
int targetH = newView.getHeight();

两者都返回 0。我在 SO 中看到了很多解决方案,但没有一个对我有用(在我的代码中你可能会看到不同方法的残余,我在过去的 3 小时里一直在尝试这些方法!)。

感谢任何帮助!

最佳答案

可以通过globalLayoutListener获取高和宽,尝试这样做:

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
           int width = imageView.getWidth();
           int height = imageView.getHeight();
           //you can add your code here on what you want to do to the height and width you can pass it as parameter or make width and height a global variable
           imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
});

希望它有用:)

关于Android:动态创建的NetworkImageView的宽度和高度返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308004/

相关文章:

android - 以编程方式创建小部件

android - 如何在android中使用volley库调用graphql post请求

Android等待截击完成并稍后更新用户界面

java - 在字节数组中查找特定字符?

android - 解析 Android Manifest 文件以使用 python 查找 uses-permission 标签

android - 减小 Android 应用程序大小

android - 为什么 PreferenceActivity 会抛出 UnsupportedEncodingException?

Android Volley : comes with a . gitignore

java - 如何在循环中发出 Volley 请求,直到出现特定响应

php - JSONException 索引超出范围 [0..1)