本文介绍了将背景图像设置为视图会拉伸我的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为视图创建了一个背景图像位图,现在视图被拉伸到背景图像的大小....

这正常吗?

这是我如何将其应用于我的视图

v.setBackgroundResource(R.drawable.backgroundgreen);

例如...

如果图像的高度为 500 像素并且视图的高度为 200px(将 wrap_content 设置为高度)将图像设置为背景后,我的视图高度变为 500 像素...

解决方案

在我看来,你面临的问题不是问题,而是 Android 如何设计布局的方式.

这意味着您可以使用 3 个默认常量值设置高度和宽度:

FILL_PARENT
View 请求的高度或宽度的特殊值.FILL_PARENT 意味着 View 希望与其父级一样大,减去父级的填充(如果有).从 API 级别 8 开始不推荐使用此值,并替换为 MATCH_PARENT.

MATCH_PARENT
View 请求的高度或宽度的特殊值.MATCH_PARENT 意味着视图想要与其父视图一样大,如果有的话减去父视图的填充.在 API 级别 8 中引入.

WRAP_CONTENT
View 请求的高度或宽度的特殊值.WRAP_CONTENT 意味着 View 想要足够大以适应它自己的内部内容,同时考虑到它自己的填充.

现在,当您将 View 的高度/宽度设置为 WRAP_CONTENT 时,您将允许视图采用足以显示视图的大小内容.背景图像也是 View 的内容,因此您的视图将显示为与图像一样大的尺寸.这不是问题,这就是它的显示方式.

好的,但在您的情况下,这对您来说是个问题,因为您有一个背景可以显示,并且不应为此拉伸视图.我可以建议几种方法:

  1. 第一个也是非常明显的:制作正确大小的图像并将它们保持在不同的位置 可绘制文件夹.

  2. 不使用常量指定视图的大小,但在 DP 中.如果有必要,为不同的大小制作不同的布局 layout 文件夹中.

  3. 你可以使用一个非常有用的设计布局是布局权重.

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?

here's how I apply it to my view

v.setBackgroundResource(R.drawable.backgroundgreen);

for instance...

if the image is 500px in heightand the view is 200px in height(being set wrap_content as height)after setting the image as background my view becomes 500px in height...

解决方案

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 by MATCH_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 the View 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 to WRAP_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 the View'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:

  1. First and very obvious: make correctly sized images and keep them in different drawable folders.

  2. Specify the size of view not using constants, but in DP. If it becomes necessary, make different layout layout folders.

  3. You can use a very useful thing for design layout is layout weight.

这篇关于将背景图像设置为视图会拉伸我的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:31