【问题标题】:ImageView in ListView that expands maximum width展开最大宽度的 ListView 中的 ImageView
【发布时间】:2012-09-05 18:00:20
【问题描述】:

为了讨论,我有一个ListView,其项目是ImageViews。我希望图像缩放到ListView 行的width,并保持它的纵横比。这是ListView的项目布局的简化版本,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="4dp" >

        <FrameLayout
            android:id="@+id/page_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:src="@drawable/some_image" />
        </FrameLayout>
    </FrameLayout>

</LinearLayout>

ImageView 的宽度是match_parent,它的所有祖先都是一样的。布局设计器显示ImageView 的尺寸跨越整个宽度,但里面的图像没有。

图像缩放正确。问题是列表视图行的高度使得宽度无法缩放到正确的大小。如果我使用fitXY,它会缩放宽度,但纵横比不正确。

有什么想法吗?

【问题讨论】:

    标签: android android-layout android-imageview


    【解决方案1】:

    如果不创建自定义视图,我找不到任何方法来完成此操作。简而言之,自定义视图将其自己的尺寸设置为其后面的可绘制对象的真实(未缩放)尺寸。

    public class ResizableImageView extends ImageView {
    
        public static interface OnSizeChangedListener {
    
            void onSizeChanged(int width, int height);
        }
    
        private OnSizeChangedListener onSizeChangedListener = null;
    
        public ResizableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
            setMeasuredDimension(width, height);
            if (onSizeChangedListener != null) {
                onSizeChangedListener.onSizeChanged(width, height);
            }
        }
    
        public void setOnSizeChangedListener(OnSizeChangedListener listener) {
            this.onSizeChangedListener = listener;
        }
    }
    

    在我的情况下,我需要获取调整后的值以调整其他视图的大小,因此是监听器接口。

    【讨论】:

      【解决方案2】:

      如果你想要保持纵横比,根据Android API Guide,我认为你必须将 android:scaleType="fitCenter" 更改为 android:scaleType="centerInside" 或者 android:scaleType=" centerCrop" 因为 fitCenter 不保持图像的纵横比。

      【讨论】:

      • 问题是适配器视图元素没有大小,直到你把东西放进去......所以它是鸡和蛋。 listview 调整它的单元格以适合子级,但其中的子图像视图希望将自身缩放到其容器的大小。
      猜你喜欢
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2023-03-28
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 2015-02-06
      相关资源
      最近更新 更多