【问题标题】:Setting same Height and width of ImageView in percentage that is in Listview以 Listview 中的百分比设置 ImageView 的相同高度和宽度
【发布时间】:2015-01-23 09:27:13
【问题描述】:

我有一个如下所示的列表视图:

但它是在 xml 中给出硬编码的高度和宽度之后。 这是我的列表视图适配器的 XML:

<LinearLayout 
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="3"    
    android:orientation="horizontal">

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="4dp"
    android:layout_marginRight="3dp"
    android:layout_marginBottom="4dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/descr_image"
    android:scaleType="centerCrop" />

</LinearLayout>

 <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="7"
    android:weightSum="7"
 android:orientation="horizontal" 
 > 

<TextView
    android:id="@+id/text"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="5"
    android:layout_gravity="left|center"
    android:gravity="center|left"
    android:layout_marginLeft="20dip"
    android:textSize="18sp" 

   />


<ImageView 
    android:id="@+id/arrow"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:layout_gravity="right|center"
    android:src="@drawable/bluearrowmap"
    android:layout_marginRight="10dp"

    />

</LinearLayout>  

我给第一个图像 2% 的宽度,但问题是现在我如何给该图像视图提供高度,就像它会从不同的设备中获取宽度一样。因为我希望这个图像是一个正方形,就像它在图片中一样. 我已经在我的适配器类中尝试过这段代码

        ViewTreeObserver vto = holder.image.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    holder.image.getViewTreeObserver().removeOnPreDrawListener(this);
                    int finalHeight = holder.image.getMeasuredHeight();
                    int finalWidth = holder.image.getMeasuredWidth();

                    holder.image.getLayoutParams().height=finalWidth;

                    return true;
                }
            }); 

我在我的适配器类的 getview 中编写此代码。它有时可以正常工作,但有时它需要向下滚动以根据宽度设置 imagview 的高度。 有人可以帮忙吗?我已经尝试了互联网上的所有解决方案,但都是徒劳的。 谢谢。

【问题讨论】:

    标签: android layout android-listview height width


    【解决方案1】:

    尝试在 LinearLayout 中正确使用权重,然后无需在运行时更改任何高度或宽度值:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"
            android:contentDescription="@string/descr_image"/>
    
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"/>
        </LinearLayout>
    
        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/>
    
    </LinearLayout>
    

    【讨论】:

    • 感谢您的快速回复实际上我正在从互联网下载图像,它们的分辨率很高。所以你的布局不能很好地工作。你有别的办法吗?
    • 那么你必须为 ImageView 使用相同的静态高度或宽度。
    • 是的,我做到了。谢谢@Haresh
    • 祝你好运亲爱的,再次很高兴帮助你。
    【解决方案2】:

    扩展图像视图并将其设置如下:

    public class SquareImageView  extends ImageView {
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
      }
    
    }
    

    【讨论】:

    • 在哪里使用这个类?你能给出任何演示代码吗?感谢您的回复
    • 在你使用 ImgaeView 的地方使用这个
    • 它正在抛出 Error inflating class com.mypackagename.SquareImageView 虽然我认为你的答案是我的解决方案。但是这个错误呢?
    • 你是否为 SquareViewClass 添加了正确的构造函数作为 ImageView 所需的。在谷歌上搜索你会得到很多这样的例子,我提供的类只显示了一半(即 wothout 构造函数)跨度>
    猜你喜欢
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2010-10-17
    • 2013-08-20
    • 2011-03-02
    相关资源
    最近更新 更多