aotian16 aotian16 - 29 days ago 9
Android Question

TextView's gravity not work when has a 9.png background

code:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:gravity="center"
android:text="H" />


@drawable/background
is a 9.png image.
If change to a common png image, gravity works.

How can i fix this? I want to make text in center.

Answer

First you have to create a drawable (drwable_example.xml) which loads the 9.png file:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <bitmap
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_vertical|fill_horizontal"
            android:src="@drawable/background" >
        </bitmap>
    </item>

</layer-list>

then you can load this as background:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="100dp"
        android:background="@drawable/drawable_example"

         >

It worked for me, hope it helps.

Comments