为视图设置背景图像可以扩展我的视图
我为视图创建了背景图像位图,现在视图被拉伸到背景图像的大小......
这是正常的吗?
<位图 xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/green" android:tileMode="重复"/>
以下是我将其应用到我的视图中的方法
v.setBackgroundResource(R.drawable.backgroundgreen);
例如,
...如果图像高度为 500 像素 视图高度为200px(将wrap_content设置为高度) 将图像设置为背景后,我的视图高度变为 500px...
I created a background image bitmap for a view and now the view is being stretched to the size of the background image....
is this normal?
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/green" android:tileMode="repeat"/>
here's how I apply it to my view
v.setBackgroundResource(R.drawable.backgroundgreen);
for instance...
if the image is 500px in height
and the view is 200px in height(being set wrap_content as height)
after setting the image as background my view becomes 500px in height...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我也遇到过同样的问题。
如果背景图像的大小大于视图的大小,则视图的大小将更改以匹配图像的大小。
解决方案
将 ImageView 的
src
设置为背景图片当然这一切都可以在代码中完成。
I have faced this same problem.
If the background image has a size that is bigger than the view's size, the view's size will change to match the image's size.
Solution
Set the
src
of the ImageView to your background imageThis all can be done in code of course.
在我看来,你面临的问题不是问题,而是Android设计布局的方式。
这意味着您可以使用 3 个默认常量值设置高度和宽度:
FILL_PARENT
View 请求的高度或宽度的特殊值
。FILL_PARENT
表示视图希望与其父级一样大,减去父级的填充(如果有)。从 API 级别 8 开始,此值已弃用,并由MATCH_PARENT
取代。MATCH_PARENT
View 请求的高度或宽度的特殊值
。MATCH_PARENT
表示视图希望与其父视图一样大,减去父视图的填充(如果有)。在 API 级别 8 中引入。WRAP_CONTENT
View 请求的高度或宽度的特殊值
。WRAP_CONTENT
意味着View
希望足够大以适应其自身的内部内容,同时考虑其自身的填充。现在,当您将
View
的高度/宽度设置为WRAP_CONTENT
时,您就允许视图采用足以显示视图内容的大小。背景图像也是View
的内容,因此您的视图将显示与图像一样大的尺寸。这不是问题,这就是它的显示方式。好的,但在你的情况下,这对你来说是一个问题,因为你有一个背景要展示,并且视图不应该为此而拉伸。我可以建议几种方法:
第一个也是非常明显的:制作正确大小的图像并将它们保持在不同的可绘制文件夹。
不使用常量指定视图的大小,但是在 DP 中。如果有必要,请为不同的大小制作不同的布局 XML 文件,并将它们保存在 布局文件夹< /a>.
设计布局时可以使用的一个非常有用的东西是布局权重.
According to me, the problem you are facing is not a problem, it is the way how Android is used to design the layouts.
This means that you can set the height and width with 3 default constant values:
FILL_PARENT
Special value for the height or width requested by a
View
.FILL_PARENT
means that the View wants to be as big as its parent, minus the parent's padding if any. This value is deprecated starting in API Level 8 and replaced byMATCH_PARENT
.MATCH_PARENT
Special value for the height or width requested by a
View
.MATCH_PARENT
means that the view wants to be as big as its parent, minus the parent's padding if any. Introduced in API Level 8.WRAP_CONTENT
Special value for the height or width requested by a
View
.WRAP_CONTENT
means that theView
wants to be just large enough to fit its own internal content, taking its own padding into account.Now, when you are setting the
View
's height/width toWRAP_CONTENT
, you are allowing the view to take that much size that is sufficient to show to view's content. The background image is also theView
's content, hence you view will be shown of as much size as the image. That's not a problem, that's how it's shown.Okay, but in your situation that's an issue for you because you have a background to show and view should not be stretched for that. I can suggest few ways:
First and very obvious: make correctly sized images and keep them in different drawable folders.
Specify the size of view not using constants, but in DP. If it becomes necessary, make different layout XML files for different sizes and keep them in layout folders.
You can use a very useful thing for design layout is layout weight.
我建议创建一个包装布局并将背景图像放在那里。我就是这样使用的,非常适合。
请参阅下面的示例
...
社交编码@AspiroTV
I suggest to create a wrapper layout and put the background image in there. i'm using it that way and fits very nicely.
see example below
...
Social Coding @ AspiroTV
原因很简单。您必须看到 View::getSuggestedMinimumWidth/Height 方法。
看到这里,你可能知道为什么背景会让视图变大,特别是为什么要为其分配 BitmapDrawable。
简单的解决方案是包装该 Drawable(例如 BitmapDrawable),然后为 getMinimumHeight() 和 getMinimumWidth() 返回 0,最好重写 getIntrinsicHeight() 和 getIntrinsicWidth() 以返回 -1。
support-v7有一个DrawableWrapper,它在必要时将调用委托给另一个drawable。您可以扩展该方法并覆盖上面讨论的方法。
如果您不使用 support-v7(哇!您太棒了),将该类复制到您的项目中也可以。
https://android.googlesource.com/platform/frameworks/support/+/1949ae9aeaadf52ad7bd7bb74ca5419c67ea7f65/v7/appcompat/src/android/support/v7/internal/widget/DrawableWrapper.java
The reason is quite simple. You gotta see the View::getSuggestedMinimumWidth/Height method.
Seeing that, you may know why the background makes a view bigger, especially why assign a BitmapDrawable to it.
and the simple solution is to wrap that Drawable (eg. BitmapDrawable), then returns 0 for getMinimumHeight() and getMinimumWidth(), and better to override getIntrinsicHeight() and getIntrinsicWidth() to returns -1.
support-v7 has a DrawableWrapper which delegates calls to another drawable when necessary. you can extends that one and override methods talked above.
and if you don't use support-v7 (WoW! you are awesome), copy that class to your project is also fine.
https://android.googlesource.com/platform/frameworks/support/+/1949ae9aeaadf52ad7bd7bb74ca5419c67ea7f65/v7/appcompat/src/android/support/v7/internal/widget/DrawableWrapper.java
它对我有用。
在你的代码中,v 是什么?它有 fill_parent 参数吗?
It's working for me.
in your code, what is v? It has params to fill_parent?
不要使用
android:tileMode="repeat"
。你的绿色绘图比你的视图更大还是更小?可以添加更多细节吗?Don't use
android:tileMode="repeat"
. Is your green drawable bigger or smaller than your view? Could add more details?在我的例子中,一个完美运行的好解决方案是扩展
View
并覆盖onMeasure()
。以下是要做的步骤:
View
,此处为例如我将使用
Button
。onMeasure()
并在底部插入代码。这将在第一个措施完成后设置后台资源。对于第二个测量事件,它将使用已经测量的参数。扩展
Button
的自定义视图的示例代码(将 Button 更改为您想要扩展的视图)这里唯一需要更改的是您想要扩展的视图类型以及
onMeasure 中的视图类型()
方法您要用于视图的背景资源。之后,只需在布局 xml 中使用此视图或以编程方式添加它。
One good solution that is working perfectly in my case is extending the
View
and overridingonMeasure()
.Here is the steps to do:
View
you want to use, here forexample I will use
Button
.onMeasure()
and insert the code at the bottom. This will set the background resource after the first measure has been done. For the second measure event, it will use the already measured paramters.Example code for a custom view which extends
Button
(change Button to the View you would like to extend)The only thing to change here is the type of view you want to extend and in the
onMeasure()
method the background resource you want to use for the view.After that, just use this view in your layout xml or add it programatically.
我修改了 Sherif elKhatib 的代码,现在这对我有用:
如果我们希望背景图片拉伸为视图图片:
如果我们希望背景图片不被拉伸,而是被剪切以适合视图图片:
I modify Sherif elKhatib's code, now this works for me:
if we want background picture be stretched as view picture:
if we want background picture not to be stretched, but to be cutted to fit view picture: