How to implement an android:background that doesn't stretch?

前端 未结 11 1096
太阳男子
太阳男子 2020-11-28 02:51

I found this great thread describing how to \"eat the cake and have it too\", i.e. use image for a Button instead of ImageButton (which doesn\'t al

相关标签:
11条回答
  • 2020-11-28 03:44

    I am using an ImageView in an RelativeLayout that overlays with my normal layout. No code required. It sizes the image to the full height of the screen (or any other layout you use) and then crops the picture left and right to fit the width. In my case, if the user turns the screen, the picture may be a tiny bit too small. Therefore I use match_parent, which will make the image stretch in width if too small.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/main_backgroundImage"
            android:layout_width="match_parent"
            //comment: Stretches picture in the width if too small. Use "wrap_content" does not stretch, but leaves space
    
            android:layout_height="match_parent"
            //in my case I always want the height filled
    
            android:layout_alignParentTop="true"
            android:scaleType="centerCrop"
            //will crop picture left and right, so it fits in height and keeps aspect ratio
    
            android:contentDescription="@string/image"
            android:src="@drawable/your_image" />
    
        <LinearLayout
            android:id="@+id/main_root"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-28 03:47

    Simply using ImageButton instead of Button fixes the problem.

    <ImageButton android:layout_width="30dp"
                 android:layout_height="30dp"
                 android:src="@drawable/bgimage" />
    

    and you can set

    android:background="@null"
    

    to remove button background if you want.

    Quick Fix !! :-)

    0 讨论(0)
  • 2020-11-28 03:48

    You can create an xml bitmap and use it as background for the view. To prevent stretching you can specify android:gravity attribute.

    for example:

    <?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/dvdr"
        android:tileMode="disabled" android:gravity="top" >
    </bitmap>
    

    There are a lot of options you can use to customize the rendering of the image

    http://developer.android.com/guide/topics/resources/drawable-resource.html#Bitmap

    0 讨论(0)
  • 2020-11-28 03:49

    Use draw9patch... included within Android Studio's SDK tools. You can define the stretchable areas of your image. Important parts are constrained and the image doesn't look all warped. A good demo on dra9patch is HERE

    Use draw9patch to change your existing splash.png into new_splash.9.png, drag new_splash.9.png into the drawable-hdpi project folder ensure the AndroidManifest and styles.xml are proper as below:

    AndroidManifest.xml:

    <application
    ...
            android:theme="@style/splashScreenStyle"
    >
    

    styles.xml:

    <style name="splashScreenStyle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:windowBackground">@drawable/new_splash</item>
    </style>
    
    0 讨论(0)
  • 2020-11-28 03:49

    One can use a plain ImageView in his xml and make it clickable (android:clickable="true")? You only have to use as src an image that has been shaped like a button i.e round corners.

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