为视图设置背景图像可以扩展我的视图

发布于 2024-12-17 05:59:48 字数 499 浏览 3 评论 0原文

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

这是正常的吗?


    <位图 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

妖妓 2024-12-24 05:59:48

我也遇到过同样的问题。

如果背景图像的大小大于视图的大小,则视图的大小将更改以匹配图像的大小。

解决方案


  1. 将视图放入相对布局中。
  2. 删除背景图像。
  3. 在相对布局中的 View 之前添加一个 ImageView
  4. 将 ImageView 的 src 设置为背景图片

    
    
        <图像视图
            安卓:layout_width =“wrap_content”
            安卓:layout_height =“wrap_content”
            android:layout_alignLeft="@+id/yourViewId"
            android:layout_alignRight="@+id/yourViewId"
            android:layout_alignTop="@+id/yourViewId"
            android:layout_alignBottom="@+id/yourViewId"
            机器人:调整ViewBounds =“真” 
            //这将允许图像以相同的比例调整大小
            android:src="@drawable/yourDrawable" />
    
        <您的观点
            android:id="@+id/yourViewId"
            。
            ..
            ... >>
    
      
    

当然这一切都可以在代码中完成。

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


  1. Put the view inside a Relative Layout.
  2. Remove the background image.
  3. Add an ImageView before the View inside the Relative Layout
  4. Set the src of the ImageView to your background image

    <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>
    

This all can be done in code of course.

我的奇迹 2024-12-24 05:59:48

在我看来,你面临的问题不是问题,而是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的内容,因此您的视图将显示与图像一样大的尺寸。这不是问题,这就是它的显示方式。

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

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

  2. 不使用常量指定视图的大小,但是在 DP 中。如果有必要,请为不同的大小制作不同的布局 XML 文件,并将它们保存在 布局文件夹< /a>.

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

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 XML files for different sizes and keep them in layout folders.

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

今天小雨转甜 2024-12-24 05:59:48

我建议创建一个包装布局并将背景图像放在那里。我就是这样使用的,非常适合。
请参阅下面的示例

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/settingstab_scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    android:background="@drawable/wareninja_wallpaper_img"
    >
<!-- your layouts and components goes here -->
</ScrollView>

...
社交编码@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

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/settingstab_scroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
    android:background="@drawable/wareninja_wallpaper_img"
    >
<!-- your layouts and components goes here -->
</ScrollView>

...
Social Coding @ AspiroTV

栖迟 2024-12-24 05:59:48

原因很简单。您必须看到 View::getSuggestedMinimumWidth/Height 方法。

protected int getSuggestedMinimumWidth() {
    return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}

protected int getSuggestedMinimumHeight() {
    return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
}

看到这里,你可能知道为什么背景会让视图变大,特别是为什么要为其分配 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.

protected int getSuggestedMinimumWidth() {
    return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
}

protected int getSuggestedMinimumHeight() {
    return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
}

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

几度春秋 2024-12-24 05:59:48

它对我有用。

< ?xml version="1.0" encoding="utf-8"?>
 < LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/LinearLayoutTest">    

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prueba);


    ((LinearLayout)findViewById(R.id.LinearLayoutTest)).setBackgroundResource(R.drawable.greenrepeat);
}

在你的代码中,v 是什么?它有 fill_parent 参数吗?

It's working for me.

< ?xml version="1.0" encoding="utf-8"?>
 < LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/LinearLayoutTest">    

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.prueba);


    ((LinearLayout)findViewById(R.id.LinearLayoutTest)).setBackgroundResource(R.drawable.greenrepeat);
}

in your code, what is v? It has params to fill_parent?

梨涡 2024-12-24 05:59:48

不要使用android:tileMode="repeat"。你的绿色绘图比你的视图更大还是更小?可以添加更多细节吗?

Don't use android:tileMode="repeat". Is your green drawable bigger or smaller than your view? Could add more details?

东北女汉子 2024-12-24 05:59:48

在我的例子中,一个完美运行的好解决方案是扩展View并覆盖onMeasure()

以下是要做的步骤:

  1. 创建一个自己的类并扩展您要使用的 View,此处为
    例如我将使用 Button
  2. 重写方法 onMeasure() 并在底部插入代码。这将在第一个措施完成后设置后台资源。对于第二个测量事件,它将使用已经测量的参数。

扩展 Button 的自定义视图的示例代码(将 Button 更改为您想要扩展的视图)

public class MyButton extends Button {

    boolean backGroundSet = false;

    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);          
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if(backGroundSet) {
            setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
            return;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        backGroundSet = true;
        setBackgroundResource(R.drawable.button_back_selector);

    }
}

这里唯一需要更改的是您想要扩展的视图类型以及 onMeasure 中的视图类型() 方法您要用于视图的背景资源。

之后,只需在布局 xml 中使用此视图或以编程方式添加它。

One good solution that is working perfectly in my case is extending the View and overriding onMeasure().

Here is the steps to do:

  1. Create an own class and extend the View you want to use, here for
    example I will use Button.
  2. Override the method 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)

public class MyButton extends Button {

    boolean backGroundSet = false;

    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);          
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if(backGroundSet) {
            setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
            return;
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        backGroundSet = true;
        setBackgroundResource(R.drawable.button_back_selector);

    }
}

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.

一笔一画续写前缘 2024-12-24 05:59:48

我修改了 Sherif elKhatib 的代码,现在这对我有用:

  1. 如果我们希望背景图片拉伸为视图图片:

     <相对布局
             安卓:layout_width =“match_parent”
             android:layout_height="wrap_content">
    
             <图像视图
                 安卓:layout_width =“match_parent”
                 安卓:layout_height =“wrap_content”
                 android:layout_alignStart="@+id/yourViewId"
                 android:layout_alignTop="@+id/yourViewId"
                 android:layout_alignEnd="@+id/yourViewId"
                 android:layout_alignBottom="@+id/yourViewId"
                 android:scaleType="fitXY"
                 android:src="@drawable/bg_very_big_picture" //>
    
             <线性布局
                 android:id="@+id/yourViewId"
                 安卓:layout_width =“match_parent”
                 安卓:layout_height =“wrap_content”
    
  2. 如果我们希望背景图片不被拉伸,而是被剪切以适合视图图片:

     <相对布局
             安卓:layout_width =“match_parent”
             android:layout_height="wrap_content">
    
             <图像视图
                 安卓:layout_width =“match_parent”
                 安卓:layout_height =“wrap_content”
                 android:layout_alignStart="@+id/yourViewId"
                 android:layout_alignTop="@+id/yourViewId"
                 android:layout_alignEnd="@+id/yourViewId"
                 android:layout_alignBottom="@+id/yourViewId"
                 android:scaleType="centerCrop"
                 android:src="@drawable/bg_very_big_picture" //>
    
             <线性布局
                 android:id="@+id/yourViewId"
                 安卓:layout_width =“match_parent”
                 安卓:layout_height =“wrap_content”
    

I modify Sherif elKhatib's code, now this works for me:

  1. if we want background picture be stretched as view picture:

         <RelativeLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
    
             <ImageView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_alignStart="@+id/yourViewId"
                 android:layout_alignTop="@+id/yourViewId"
                 android:layout_alignEnd="@+id/yourViewId"
                 android:layout_alignBottom="@+id/yourViewId"
                 android:scaleType="fitXY"
                 android:src="@drawable/bg_very_big_picture" />
    
             <LinearLayout
                 android:id="@+id/yourViewId"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
    
  2. if we want background picture not to be stretched, but to be cutted to fit view picture:

         <RelativeLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
    
             <ImageView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_alignStart="@+id/yourViewId"
                 android:layout_alignTop="@+id/yourViewId"
                 android:layout_alignEnd="@+id/yourViewId"
                 android:layout_alignBottom="@+id/yourViewId"
                 android:scaleType="centerCrop"
                 android:src="@drawable/bg_very_big_picture" />
    
             <LinearLayout
                 android:id="@+id/yourViewId"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文