我有一个列表视图。每行是一个图像和文本。我想缩放图像,使其适合行高,即
- 图像不应导致行高更大
- 图像高度应与行高相同
- 应计算图像宽度,以保持原始宽度高度比
我想出的所有解决方案似乎都太复杂了,我认为这是一个相对常见的要求,所以我想检查一下我是否遗漏了什么——你知道如何以更简单的方式实现这一点吗?
以下是我考虑的解决方案。
解决方案1:
ListView 项目的嵌套视图并设置 android:layout_height="match_parent"。图像已正确调整大小,但 ImageView 占用宽度,就好像它没有调整大小一样。(见下图,我加了黑色背景,看看ImageView占用了多少空间。)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/Image"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:background="#000000"
android:padding="1dp"/>
<TextView
android:id="@+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/Image" />
</LinearLayout>
</RelativeLayout>
解决方案 2: 使用
ViewTreeObserver 观察者 = MyTextView.getViewTreeObserver() observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw(){
observer.removeOnPreDrawListener(this);
// Finding out height of TextView and then calculating width and height of image and setting size of ImageView. Of course, I could also get observer from ImageView and then just set width as height is correct
}
}
这似乎太复杂了。
解决方案3:
使用,例如,Paint.FontMetrics 来计算高度,但我还需要找出使用的字体(例如,系统一......) ListView 可能有一些填充等,所以有很多东西要检索。
请问还有其他更简单的解决方案吗?
编辑 - 澄清
图像具有最大值。大小,如果达到,将停止 ImageView 大小的任何进一步增加。