Search code examples
androidbackgroundnine-patch

9 patch shrinking issue


I'm having issues with a 9patch image. The layout is set to 50dp with a 9patch background, but when loaded on small screens the layout is larger than 50dp.

However I have another layout with the same 9patch background (menuBtn) and that doesn't expand so I'm not sure what is making the 9patch misbehave on this one layout.

SS:
enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:padding="0dp"
    android:background="@drawable/actionbar">
    <RelativeLayout
        android:id="@+id/menuBtn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/actionbar"
        android:clickable="true">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/actionbar_menu"/>
    </RelativeLayout>
    <ImageView
        android:id="@+id/div1"
        android:layout_width="1px"
        android:layout_height="50dp"
        android:layout_toRightOf="@id/menuBtn"
        android:src="@drawable/actionbar_divider"/>
</RelativeLayout>

9patch img:
enter image description here


Solution

  • Figured it out, it's a weird bug? Anyway the 9patch is taller than 50dp on the small screen size so with the base background set to actionbar, it doesn't scale.

    However if I nest another RelativeLayout inside the main RelativeLayout and set that background to actionbar, then it scales just fine. Not sure why the original RelativeLayout doesn't.

    Example:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="0dp">
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:padding="0dp"
            android:background="@drawable/actionbar">
        </RelativeLayout>
    </RelativeLayout>
    

    This code works, so yeah...