【问题标题】:Setting ImageView width and height before drawing in a ListView adapter在 ListView 适配器中绘制之前设置 ImageView 的宽度和高度
【发布时间】:2013-05-29 21:49:22
【问题描述】:

我有一个ListView 的适配器是ImageViews 的列表。我正在使用拉伸使图像填充图像视图,这样我就可以拍摄较小的图像并在屏幕上放大它们,但是ImageView 通常只使用wrap_content,这是一个问题,因为图像只是显示为它们正常的宽度和高度。有什么方法可以在绘制视图之前设置视图的高度和宽度,因为在这种情况下,在绘制视图之后我无法控制视图。这是我的另一种方法:

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
    String currentImage  = getItem(position);
    ScaleType scaleType = ScaleType.FIT_CENTER;
    float screenWidth = parent.getResources().getDisplayMetrics().widthPixels;

    if (convertView == null) {
        convertView = new ImageView(parent.getContext()); 
    }
  //         WHAT I WOULD LIKE TO BE ABLE TO DO, but this returns null pointer exception
 //     convertView.getLayoutParams().width = (int) screenWidth;
//      convertView.getLayoutParams().height = (int) ((float)(screenWidth/currentImage.getWidth())*currentImage.getHeight());

    ((ImageView) convertView).setScaleType(scaleType);
    ((ImageView) convertView).setImageBitmap(MainActivity.cache.getBitmap(currentImage));
    return convertView;
}

【问题讨论】:

标签: android android-listview android-imageview


【解决方案1】:

someView.setHeight() and someView.setWidth() 这样的东西怎么样?还是someView.setLayoutParams()?您可以将其中任何一个添加到覆盖的 getView() 回调中,它应该可以解决您的问题。

您也可以Create a Custom View 并覆盖诸如 getMeasuredWidthAndState() 之类的内容。 (我认为这是正确的方法之一,但我不能百分百确定。)您可以创建一个 width 类变量和一个 height 类变量,您的自定义 ImageView 的所有实例都将使用它们。但是,如果您只是想设置布局的宽度和高度,那可能有点多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2023-03-23
    • 2021-06-14
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多