Setting a background image to a view stretches my view

前端 未结 7 2076
梦如初夏
梦如初夏 2020-12-05 17:54

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?



        
相关标签:
7条回答
  • 2020-12-05 18:21

    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.

    0 讨论(0)
提交回复
热议问题