开发者

Actual view's height is not fitted to proper content

开发者 https://www.devze.com 2023-03-09 13:36 出处:网络
I have an ImageView, it shows an image from resource. ImageView\'s widht is fixed (60dp). Height is set as wrap_content.

I have an ImageView, it shows an image from resource. ImageView's widht is fixed (60dp). Height is set as wrap_content. The image is resized to fit to this width (saving aspect ration - this is perfect)

The problem is that android sets actual height to the image's heights before resizing. But I want this开发者_如何学JAVA image to have exactly 60dp in width and height should be equal to image's height after resizing.

Here is layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView  
        android:layout_width="60dp" 
        android:layout_height="wrap_content" 
        android:src="@drawable/sample"
        android:background="#F0F0"/>
</LinearLayout>

Actual view's height is not fitted to proper content


try android:adjustViewBounds="true"


The solution is quite simple, I just added atribute android:adjustViewBounds="true" for ImageView tag.


There are two solutions

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView  
        android:layout_width="60dip" 
        android:layout_height="wrap_content" 

        android:background="@drawable/thumb"
        android:adjustViewBounds="true"/>
</LinearLayout>
0

精彩评论

暂无评论...
验证码 换一张
取 消