2014-09-23 57 views
1

大家好,感谢您的帮助。android imageview wrap_content

我在Android Studio中有一个项目。

我在垂直LinearLayout中使用ImageView。

这里是我的参数;

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView2" 
    android:src="@drawable/myimg" 
/> 

你可以看到我的形象楼下(红色部分)和ImageView的边界(蓝线) 没有与宽度没有问题的,这是我的布局%100,但我想我的形象,以适应区域同时保持纵横比。正如你在图像中看到的那样有白色空间,ImageView高度比我的图像更大,我如何在ImageView中适合图像?

我想要这个,因为我希望该图像适合宽度,即使手机是纵向或横向。

enter image description here

+0

相反的android:SRC,使用Android:背景,然后根据您的需要check.It可以得到所需的效果。 – Ram 2014-09-23 13:00:51

回答

12

我找到了解决办法 android:adjustViewBounds="true"

0

imageview的大小correct.You必须相应地扩大你的图片。 ImageView有缩放的属性。试试这个android:scaleType="fitXY"并让我知道。

+0

当我使用“fitXY”时,图像的纵横比发生变化,图像看起来很糟糕。 – yucel 2014-09-23 18:30:31

0

你的代码是写的,但我不知道父母的布局,你的图像背景是白色的&红色的部分在这个背景中。

这里是与父布局相对

<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" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MyActivity"> 



<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:src="@drawable/red" 
    /> 

enter image description here

0

对于ImageViewandroid:scaleType="fitXY"意愿损失aspectration代码。最好使用android:scaleType="fitCenter"并为imageView transparent设置背景。

如果你不想保持aspectration,然后setbackground()fitxy将做您的要求

0

在ImageView的,有中心作物的选择 -

android:scaleType="centerCrop" 

它的作用是,它会从中心裁剪图像。输出会像 -

resized