user1825535
user1825535

Reputation: 23

Trouble scaling 9patch images evenly

I'm attempting to make some simple 9patch PNG photos for using with ImageButton views, however they are not scaling correctly. For example, here is the 9patch PNG image I'm using:

Enter image description here

However this is how it scales:

Enter image description here

This is my XML for the buttons:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"  >

<ImageButton
    android:id="@+id/add_challenge1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/lockedbutton1"
    android:onClick="add1" />

<ImageButton
    android:id="@+id/add_challenge2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/lockedbutton1"
    android:onClick="add2" />

<ImageButton
    android:id="@+id/add_challenge3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/lockedbutton1"
    android:onClick="add3" />

<ImageButton
    android:id="@+id/add_challenge4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/lockedbutton1"
    android:onClick="add4" />

<ImageButton
    android:id="@+id/add_challenge5"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/lockedbutton1"
    android:onClick="add5" />                          
</LinearLayout> 

I'm really not sure why the images are scaling with the content (the lock) a lot further to the right than the left, as opposed to being stretched evenly and keeping the lock in the center. Any ideas as to why this is happening? Thanks a bunch of anybody could offer some help.

Upvotes: 2

Views: 523

Answers (1)

Emil Davtyan
Emil Davtyan

Reputation: 14089

Take the lock image out of the 9-patch, make a separate PNG with lock and define it with android:src instead. To center in button use android:layout_gravity=center.

Upvotes: 1

Related Questions