简体   繁体   English

设置ImageView的宽度和高度相对于文本高度

[英]Setting ImageView width and height relative to text height

I have a ListView. 我有一个ListView。 On each row is an image and text. 每行上都有一个图像和文本。 I want to scale image so it fits the row height ie 我想缩放图像,使其适合行高,即

  • image shouldn't cause the row height is bigger 图像不应导致行高更大
  • image height should be the same as the row height 图像高度应与行高度相同
  • image width should be calculated so the original width height ratio is maintained 应计算图像宽度,以保持原始宽度与高度的比例

All solutions that I figured out seem too complex for which I'd assume is a relatively common requirement so I'd like to check if I'm missing something - do you have an idea how to achieve this in a simpler way ? 我想出的所有解决方案似乎都太复杂了,我认为这是一个相对普遍的要求,因此我想检查一下是否缺少某些东西-您是否知道如何以更简单的方式实现这一目标?

The following are solutions I considered. 以下是我考虑过的解决方案。

Solution 1: 解决方案1:

Nested views for ListView item and set android:layout_height="match_parent". ListView项目的嵌套视图,并设置android:layout_height =“ match_parent”。 Image is resized correctly but ImageView occupies the width as if it was not resized. 正确调整了图像的大小,但是ImageView占据了宽度,就好像它没有调整大小一样。 (See the following picture. I added black background to see how much space occupies 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>

Solution 2: Using 解决方案2:使用

ViewTreeObserver observer = MyTextView.getViewTreeObserver() observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { ViewTreeObserver观察者= MyTextView.getViewTreeObserver()观察者.addOnPreDrawListener(新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
}

} }

This seems like too complicated. 这似乎太复杂了。

Solution 3: 解决方案3:

Use, eg, Paint.FontMetrics to calculate height but I'd also need to find out the font used (eg, system one ...) Also ListView probably has some padding, etc. so kind of lots of things to retrieve. 使用例如Paint.FontMetrics来计算高度,但我还需要找出所使用的字体(例如,系统一...)。此外,ListView可能还有一些填充等,因此需要检索很多东西。

Any other simpler solution please ? 还有其他更简单的解决方案吗?

Edit - clarification 编辑-澄清

The image has the max. 图片最大 size, which if reached, stops any further increasing of the size of ImageView. 大小,如果达到该大小,将停止ImageView大小的任何进一步增加。

If you are wanting to set the width and height of the ImageView based on the width and height, you can always get the width and height of the TextView programmatically, then implement your own algorithm to scale the ImageView accordingly. 如果要基于宽度和高度设置ImageView的宽度和高度,则始终可以以编程方式获取TextView的宽度和高度,然后实施自己的算法来相应地缩放ImageView。

To get the width and height from a textView, let's call it item_textView , it's fairly simple: 要从item_textView获取宽度和高度,我们将其item_textView ,这很简单:

TextView item_textView = findViewById(R.id.item_textView);
int text_width = item_textView.getWidth();
int text_height  = item_textView.getHeight();

At this point, you can perform whatever math you need to perform to scale the imageView down to the correct size. 此时,您可以执行将imageView缩小到正确大小所需的任何数学运算。 To set the size of the imageView: 设置imageView的大小:

my_imageView.getLayoutParams().width = text_width/2;
my_imageView.getLayoutParams().height = text_height/2;

Since you defined the parent linear layout height as wrap content it will take the size of its content and may vary based on its children element dimensions. 由于您将父级线性布局高度定义为环绕内容,因此它将采用其内容的大小,并可能根据其子元素尺寸而有所不同。

Try to define exact size for the image in your row layout. 尝试为行布局中的图像 定义确切的尺寸 If you can define its width and height as 40dp or 48dp you might be able to achieve your goal. 如果可以将其宽度和高度定义为40dp或48dp,则可以实现您的目标。

If all elements depend on each other's size you might be able to see variations. 如果所有元素都依赖于彼此的大小,则可能会看到变化。 Try to define the avatar/image sizes in specific dp . 尝试在特定dp中定义头像/图像大小。

I tried to tweak your layout and got some results. 我试图调整您的布局并获得了一些结果。 Check out the following code. 查看以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">
    <ImageView
        android:id="@+id/earthquake_magnitude"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:background="@android:color/black"
        android:fontFamily="sans-serif-medium"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_weight="1"
        android:text="CHROME" />
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM