如何适当调整图像大小并在android中设置其高度和宽度

时间:2015-05-07 18:10:00

标签: android image

我知道这可能是一个愚蠢的问题,但我打赌你们中的一些人之前有这个问题。从截图中,我试图在一个活动中设置一个图像。但是我的图像带来了这些空间我不知道如何摆脱它们。我的意思是有一种方法,无论是在xml中还是以编程方式调整图像大小以适应高度和宽度?简而言之,有没有办法可以摆脱上下这些空间? enter image description here

我的xml:



<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"
    tools:context="com.snappy.stevekamau.cosmeticsapp.Categories.Lips">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"></include>

    <ImageView
        android:id="@+id/imageTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/white_rectangle"
        android:src="@drawable/img4" />

    <TextView

        android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

是的,可以通过一行代码完成。这就是诀窍:))

android:adjustViewBounds="true"

ImageView

中添加此内容
<ImageView
        android:id="@+id/imageTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/white_rectangle"
        android:src="@drawable/img4" />

答案 1 :(得分:1)

要使位图填充宽度和高度,您可以将android:scaleType="fitXY"添加到ImageView。这样,您的位图将在x和y中独立缩放,使您的位图完全填充ImageView