Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

android - Setting a background image to a view stretches my view

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...