How can I scale the background of a button?

馋奶兔 提交于 2019-12-05 03:40:43
Pathos

You could use an ImageButton and try android:scaleType

ImageView.ScaleType

I don't hink you can from xml. There's no xml attribute that can do scaling for buttons. You can do it programatically though, in the following way :

Bitmap originalBitmap= BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap scaledBitmap=Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
txt.setBackgroundDrawable(new BitmapDrawable(bit));

You could scale down the actual graphic that the drawable points to.

If you don't want to deal with Bitmap.createScaledBitmap then it seems to me that the best way to do it is to create your own component. You can extend RelativeLayout and put ImageView into it, for example.

Shn_Android_Dev

I know this is an old post, but I did something like this:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:padding="16dp"
    android:id="@+id/XXXXXXXXXXXX"
    android:orientation="horizontal">
    <ImageView
        android:src="@drawable/XXXXXXXXXXXX"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="centerInside"
        android:id="@+id/XXXXXXXXXXXXXXXX"/>
    <TextView
        android:id="@+id/XXXXXXXXXXXXXX"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="XXXXXXX"
        android:layout_gravity="center"/>
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!