9patch causing weird layout_height issue

Go To StackoverFlow.com

0

I have a fairly simple layout with an image, and some textviews wrapped in a vertical LinearLayout. Whenever I set the background of this layout to be a 9patch, the height of the layout seemingly gets set to the height of the 9patch (even though the 9patch seems to successfully stretch to be the height of the layout.)

If I use a solid background color, the height of the layout works as intended.

Here's a screenshot with the 9patch in place:

enter image description here

Here's a screenshot with a solid background instead of the 9patch (notice how the text is centered in the layout now):

enter image description here

And finally, here's the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/show_gray"
    android:gravity="center_vertical"
    android:orientation="horizontal" android:weightSum="5">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:scaleType="centerCrop"
        android:src="@drawable/temp_featured_banner" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FF0000"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="30dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textColor="@color/white" />
    </LinearLayout>

</LinearLayout>

... and just in case, here's a screenshot of draw9patch showing my 9patch drawable:

enter image description here

I suspected that the LinearLayout wasn't taller than the natural size of the 9patch (so there was nothing to stretch..), so I reduced the 9patch in height by 50%. The problem still existed.

Any ideas?

2012-04-04 16:52
by NPike
Have you tried setting the vertical fill area of your nine-patch - JRaymond 2012-04-04 16:56
@JRaymond there are two black dots on the very left-top and left-bottom of the image. Is that what you mean - NPike 2012-04-04 17:17
No... the top and left of the nine patch refer to the 'stretchable' area of the image. The right and bottom of the nine-patch define where the content should go - currently, you're just telling it to guess (and it guesses by putting all of the content in the line designated by the top left dot). from what I can tell from your image, you probably want a solid black line top to bottom (leaving out the corners, of course) on the right of the nine-patch, and a matching dot to your top-right black dot on the botto - JRaymond 2012-04-04 17:31
See http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patc - JRaymond 2012-04-04 17:34
Thanks, that seems to have done the trick! If you post it as an reply I will give you the answer - NPike 2012-04-04 20:33
Rewrote as an answer - you're welcome - JRaymond 2012-04-04 20:36


1

The top and left of the nine patch refer to the 'stretchable' area of the image. The right and bottom of the nine-patch define where the content should go - currently, you're just telling it to guess (and it guesses by putting all of the content in the line designated by the top left dot). from what I can tell from your image, you probably want a solid black line top to bottom (leaving out the corners, of course) on the right of the nine-patch, and a matching dot to your top-right black dot on the bottom. This tells the OS where to put the content in relation to the image so that it better knows how to stretch the image.

2012-04-04 20:35
by JRaymond


1

In the 9-patch program, draw lines on the bottom and right sides to choose areas that can be "collapsed". Might fix your issue.

http://developer.android.com/guide/developing/tools/draw9patch.html

2012-04-04 16:56
by MrLeap
Ads