ImageView不能拉伸(Android)?

4
我在RelativeLayout里面有一个ImageView。ImageView的源是一个.png文件。问题在于,当在不同分辨率的设备上测试时,Android没有对图像进行缩放。我在Android文档中读到,Android会自动缩放图像以填充屏幕,但这并没有发生。可能出了什么问题?我是Android的新手,如果这个问题很蠢的话请见谅,我已经搜索了很多但是找不到解决方案。
编辑:XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >

<ImageView
        android:id="@+id/imageview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerInParent="true"
        android:clickable="false"
        android:scaleType="fitXY"
        android:contentDescription="@string/p"

        android:src="@drawable/example" />
</RelativeLayout>

编辑2:我需要拉伸的XML布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:theme="@android:style/Theme.Black.NoTitleBar" 
    android:gravity="center">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:clickable="false"

        android:contentDescription="@string/p"

        android:src="@drawable/volbar" />



    <ImageView

        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:layout_alignParentLeft="true"
        android:contentDescription="@string/p"

        android:src="@drawable/head" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="33dp"
        android:layout_marginRight="19dp"
        android:text="@string/press_to_update" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView2"
        android:clickable="false"
              android:contentDescription="@string/p"

        android:src="@drawable/frag11"
         />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"

        android:layout_below="@+id/imageView9"
        android:clickable="false"
        android:contentDescription="@string/p"

        android:src="@drawable/base" />
    </RelativeLayout>

检查图像中是否存在透明填充。 - stinepike
@StinePike 没有填充在这个图像中。 - vergil corleone
@vergilcorleone 不是指拉伸,你是希望图片占据整个屏幕吗? - Pragnani
@vergilcorleone 然后将高度和宽度设置为 match_parent - Pragnani
@vergilcorleone,你到底想要什么?发布你的设计..然后我们可以理解。 - stinepike
显示剩余2条评论
3个回答

11

在布局中使用android:scaleType="fitXY"

编辑

如果您想让图像占据整个屏幕,那么请使用

    android:layout_width="match_parent"
    android:layout_height="match_parent"

建议使用“match parent”代替“fill parent”,因为“fill parent”已经被弃用。 - Pragnani
@StinePike 我稍后会在一个包含多个ImageView的布局中使用这个概念,添加match_parent不起作用,还有其他方法吗? - vergil corleone
@StinePike 添加了在模拟器上运行的设计。它由多个图像视图组成,正如您所看到的,它没有拉伸以填充屏幕。 - vergil corleone
好的,您可以使用LinearLayout与权重而不是wrap_content... 或者您可以使用更大的图像... 因为您的图像不够大... 所以视图也显示得很小。 - stinepike
让我们在聊天中继续这个讨论 - stinepike
显示剩余7条评论

1
请参考以下内容:

**

<ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@drawable/ic_launcher"
                ***android:scaleType="fitXY"***
                android:layout_alignParentRight="true"
                />

**

ImageView无法填充父元素

希望这能帮到你...


0

android:layout_width="match_parent"

android:layout_height="match_parent"

 >

    android:id="@+id/imageview1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:clickable="false"

    android:contentDescription="@string/p"

    android:background="@drawable/example" />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接