本文介绍了缩放背景图像以包裹布局内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,其中包含一些文本字段,并在我的活动顶部显示了一个背景图像.我希望背景图像可以缩放以包裹内容(不关心纵横比).但是,图像比内容大,因此布局改为包裹背景图像.这是我的原始代码:

I have a layout that contains some text fields and has a background image that's displayed at the top of my activity. I'd like the background image to scale to wrap the content (don't care about aspect ratio). However, the image is larger than content, so the layout instead wraps the background image. Here's my original code:

<RelativeLayout
    android:layout_width="fill_parent"
    android:id="@+id/HeaderList"
    android:layout_gravity="top"
    android:layout_height="wrap_content"
    android:background="@drawable/header">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/NameText"
        android:text="Jhn Doe"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="4dp"
        android:paddingTop="4dp"
    />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="#FFFFFF"
        android:layout_alignParentLeft="true"
        android:id="@+id/HoursText"
        android:text="170 hours"
        android:textSize="23sp"
        android:layout_below="@+id/NameText"
        android:paddingLeft="4dp"
    />
</RelativeLayout>

在搜索了其他一些问题后,我发现了这两个:

After searching through some other questions, I found these two:

如何包装内容视图而不是背景可绘制?

缩放可绘制或背景图像?

基于此,我创建了一个带有 ImageView 显示背景的 FrameLayout.不幸的是,我仍然无法让它工作.我希望背景图像的高度随着文本视图的大小而缩小/扩展,但是使用 FrameLayout,ImageView 适合其父级的大小,我找不到使父级适合的方法文本视图布局的大小.这是我更新的代码:

Based on this, I created a FrameLayout w/ an ImageView showing the background. Unfortunately, I still can't get it to work. I want the height of the background image to shrink/expand w/ the size of the text views, but with the FrameLayout, the ImageView fits to the size of it's parent, and I can't find a way to make the parent fit to the size the text view layout. Here's my updated code:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView android:src="@drawable/header"
        android:layout_width="fill_parent"
        android:scaleType="fitXY"
        android:layout_height="fill_parent"
        />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:id="@+id/HeaderList"
        android:layout_gravity="top"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/NameText"
            android:text="John Doe"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:paddingLeft="4dp"
            android:paddingTop="4dp"
            />
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="#FFFFFF"
            android:layout_alignParentLeft="true"
            android:id="@+id/HoursText"
            android:text="170 hours"
            android:textSize="23sp"
            android:layout_below="@+id/NameText"
            android:paddingLeft="4dp"
            />
    </RelativeLayout>
</FrameLayout>

有人对如何最好地使图像缩放到某些布局的内容大小有任何建议吗?我不关心图像的纵横比,因为这无关紧要,我只是想让它填满背景.

Does anybody have any suggestions for how best to make an image scale to the size of the contents of some layout? I'm not concerned with the aspect ratio of the image, as it won't matter, I just want it to fill the background.

谢谢!

推荐答案

我遇到了同样的问题(我认为),我能找到的唯一解决方案是:

I had the same problem (I think), and the only solution I could find was this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/some_image"
        android:layout_alignTop="@+id/actual_content"
        android:layout_alignBottom="@id/actual_content"
        android:layout_alignLeft="@id/actual_content"
        android:layout_alignRight="@id/actual_content"
        />

    <LinearLayout
        android:id="@id/actual_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        >

       <!-- more stuff ... -->

    </LinearLayout>

</RelativeLayout>

这篇关于缩放背景图像以包裹布局内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:47
查看更多