Android 将背景图像设置为视图会拉伸我的视图

Android 将背景图像设置为视图会拉伸我的视图,android,view,background,Android,View,Background,我为一个视图创建了一个背景图像位图,现在视图被拉伸到背景图像的大小 这正常吗 例如 如果图像高度为500px 视图的高度为200px(将包裹内容设置为高度) 将图像设置为背景后,我的视图高度将变为500px…这对我有效 < ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orienta

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

这正常吗

例如

如果图像高度为500px 视图的高度为200px(将包裹内容设置为高度) 将图像设置为背景后,我的视图高度将变为500px…

这对我有效

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

在你的代码中,v是什么?它有参数来填充父对象?

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

我也遇到过同样的问题

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

解决方案


  • 将视图放在相对布局内
  • 删除背景图像
  • 在相对布局内的视图之前添加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>
    
    
    


  • 当然,这一切都可以通过代码完成。

    我建议创建一个包装器布局,并将背景图像放在其中。我就是这样用的,很合身。 见下面的例子

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



    根据我的说法,你面临的问题不是问题,而是Android如何设计布局

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

    填充父对象
    视图要求的高度或宽度的特殊值
    FILL_PARENT
    表示视图希望与其父视图一样大,减去父视图的填充(如果有)。此值从API级别8开始被弃用,并由
    MATCH\u PARENT
    替换

    匹配父项
    视图要求的高度或宽度的特殊值
    MATCH_PARENT
    表示视图希望与其父视图一样大,减去父视图的填充(如果有)。在API级别8中引入

    包装内容
    视图要求的高度或宽度的特殊值
    WRAP_CONTENT
    意味着
    视图要足够大,以适合自己的内部内容,并考虑到自己的填充

    现在,当您将
    视图的高度/宽度设置为
    WRAP_CONTENT
    时,您允许视图的大小足以显示视图的内容。背景图像也是
    视图
    的内容,因此您的视图将显示与图像大小相同的大小。这不是问题,这就是它的表现

    好的,但在你的情况下,这对你来说是个问题,因为你有一个背景要展示,而不应该为此而过度关注。我可以提出以下几种方法:

  • 首先也是非常明显的一点:制作尺寸正确的图像,并将它们保存在不同的位置

  • 指定视图的大小,不使用,但使用。如果有必要,为不同的大小制作不同的布局XML文件,并将其保存在文件夹中

  • 你可以用一个非常有用的东西来设计布局


  • 原因很简单。您必须查看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

    简单的解决方案是包装该可绘制文件(例如BitmapDrawable),然后为getMinimumHeight()和getMinimumWidth()返回0,最好将getIntrinsicHeight()和getIntrinsicWidth()重写为返回-1

    support-v7有一个DrawableWrapper,它在必要时将调用委托给另一个DrawableWrapper。您可以扩展该方法并覆盖上面提到的方法

    如果您不使用support-v7(哇!您太棒了),那么将该类复制到您的项目中也可以


    在我的例子中,一个很好的解决方案是扩展
    视图并覆盖
    onMeasure()

    以下是要执行的步骤:

  • 创建自己的类并扩展要使用的
    视图
    示例我将使用
    按钮
  • 重写方法
    onMeasure()
    ,并在底部插入代码。这将在第一次测量完成后设置后台资源。对于第二个测量事件,它将使用已测量的参数
  • 扩展
    按钮的自定义视图的示例代码(将按钮更改为要扩展的视图)

    这里唯一需要更改的是要扩展的视图类型,以及在
    onMeasure()
    方法中要用于视图的后台资源


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

    我的背景比视图大,但如果以后添加更多内容,视图可能会被拉伸…v是listview中的自定义listitem,其根视图是linearlayout。。。在您的测试中,视图是比背景大还是比背景小?因为我的背景比视野要大…我不能让它工作。。。仍然显示完整图像。。。使用完全相同的代码:s我是否遗漏了什么?您是否将
    RelativeLayout
    YourView
    的内容都包装在RelativeLayout:
    android:layout\u width=“fill\u parent”
    中?因为我希望背景垂直填充屏幕。。对我来说,视野的高度是个问题…:(//edit刚刚尝试将其设置为包装内容,但仍然是一样的…我刚才在代码中使用此示例时添加了一条附加注释:根据视图和背景图像的纵横比以及所需的目标设计,您可能最终需要添加安卓:scaleType=“fitXY”到ImageView以确保你的背景被完全覆盖。弗兰肯斯坦你是对的。但是在android中,不管
    <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>
    
    <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>
    
    protected int getSuggestedMinimumWidth() {
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }
    
    protected int getSuggestedMinimumHeight() {
        return (mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
    }
    
    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);
    
        }
    }