简体   繁体   English

android imageview wrap_content

[英]android imageview wrap_content

Hello everyone and thanks for your help. 大家好,谢谢您的帮助。

I have a project in Android Studio. 我在Android Studio中有一个项目。

I am using an ImageView inside vertical LinearLayout. 我在垂直LinearLayout中使用ImageView。

Here are my parameters; 这是我的参数;

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

You can see my image down below (red block) and imageview borders (blue line) there is no problem with width, it is %100 of my layout, but I want my image to fit the area while keeping the aspect ratio. 您可以在下方(红色块)和imageview边框(蓝色线)下方看到我的图像,宽度没有问题,它是布局的%100,但是我希望我的图像适合该区域,同时保持长宽比。 as you can see in image there are white spaces, the ImageView height is bigger than my image, how can I fit image in ImageView? 正如您在图像中看到的那样,存在空白,ImageView的高度大于我的图像,如何在ImageView中容纳图像?

I want this because I want that image to fit the width even the phone is portrait or landscape. 我想要这个是因为即使手机是纵向或横向,我也希望该图像适合宽度。

在此处输入图片说明

我找到了解决方案android:adjustViewBounds="true"

The size of the imageview is correct.You have to scale your images accordingly. imageview的大小是正确的。您必须相应地缩放图像。 ImageView has property of scaling.Try this android:scaleType="fitXY" and let me know. ImageView具有缩放属性。请android:scaleType="fitXY"并让我知道。

your code is write but i don't know about parent layout and you image background is white & red part is middle in this background. 您的代码是写的,但我不知道父级布局,您的图像背景是白色,红色部分在此背景中间。

Here is code with parent layout relative 这是与父布局相对的代码

<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"
     />

在此处输入图片说明

For ImageView , android:scaleType="fitXY" will loss the aspectration. 对于ImageViewandroid:scaleType="fitXY"将失去长宽比。 Its better to use android:scaleType="fitCenter" and set the background for the imageView transparent . 最好使用android:scaleType="fitCenter"并将android:scaleType="fitCenter"的背景设置为imageView transparent

If you dont want to keep aspectration, then setbackground() or fitxy will do your requirement 如果您不想保持纵横比,则setbackground()fitxy将满足您的要求

In imageview, there is a option of center crop - 在imageview中,有一个中心裁切选项-

android:scaleType="centerCrop"

What it will do is that it will crop the image from center. 它会做的是从中心裁剪图像。 The output will be like - 输出将像-

调整大小

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

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