我为 View 创建了背景图像位图,现在 View 被拉伸(stretch)到背景图像的大小......
这正常吗?
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/green" android:tileMode="repeat"/>
这是我如何将它应用到我的 View
v.setBackgroundResource(R.drawable.backgroundgreen);
比如……
如果图像高度为 500 像素 并且 View 高度为 200 像素(将 wrap_content 设置为高度) 将图像设置为背景后,我的 View 高度变为 500px...
最佳答案
我也遇到过同样的问题。
如果背景图像的大小大于 View 的大小,则 View 的大小将更改以匹配图像的大小。
解决方案
- 将 View 放在相对布局中。
- 移除背景图片。
- 在相对布局内的 View 之前添加一个 ImageView
将ImageView的
src
设置为你的背景图片<RelativeLayout . . . > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/yourViewId" android:layout_alignRight="@+id/yourViewId" android:layout_alignTop="@+id/yourViewId" android:layout_alignBottom="@+id/yourViewId" android:adjustViewBounds="true" //this will allow the image to resize with same proportions android:src="@drawable/yourDrawable" /> <YourView android:id="@+id/yourViewId" . .. ... /> </RelativeLayout>
当然,这一切都可以在代码中完成。
关于android - 将背景图像设置为 View 会拉伸(stretch)我的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8202647/