CooL i3oY
CooL i3oY

Reputation: 800

How to fit layout background Image

I wanna use one image ( 1280x800 ) for two device with different screen size and same density .

Screen sizes are 1280x800 and 320x480 .

image is nine patch ( with content area ) . I want to use image for a row's background , that have a textview . and don't use manual padding for text ( i wanna use content area ) .

if i use this xml :

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pak.name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@layout/menu_selector"
android:gravity="right|center_vertical">

<pack.name.TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:text="blah blah"/>

output in emulator is:

http://ivm.neru9.com/pic/2ccf95a7f2e1.png

and if i use this xml:

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pack.name."
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="15dp"
android:paddingLeft="15dp">

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@layout/menu_selector"
    android:scaleType="fitCenter"
    />

<pack.name..TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:gravity="right|center_vertical"
    android:text="blah blah blah"/>

output is:

http://ivm.neru9.com/pic/f20f7f5d2e7a.png

and this is listview xml (that i think not important for this question) :

<ListView
    android:id = "@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:divider="@null"
    android:dividerHeight="3dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:cacheColorHint="@android:color/transparent"
    android:listSelector="@android:color/transparent"
    />

and its menu_selector xml code that is in layout folder:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" />
<!-- selected -->
<item android:state_selected="true" android:drawable="@drawable/bg_menu_focused" />
<!-- selected & pressed -->
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/bg_menu_pressed" />
<!-- default -->
<item android:drawable="@drawable/bg_menu" /> 

Upvotes: 3

Views: 3903

Answers (2)

rajpara
rajpara

Reputation: 5203

please check out this project,

basically problem is your 9 patch image, i just modify your image for desire performance for your problem.

i will suggest you to use bg_menu277x77 for 1280x800 and bg_menu144x40 for 320x480 resolution from above linked project. Just for testing you can interchange both image for both resolution and try.

Bellow i try my best to explain 9 patch image, other great tutorial & information you can google it :)

enter image description here

Upvotes: 2

karn
karn

Reputation: 6033

If i am understanding your requirement correctly then you want a list view and each row of the list view contains a background and a text view to display some text.
You don't have to use an image view for this purpose. All you need to do is to place a background for the layout container and a text view.

Here is how you can achieve this:

<RelativeLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res/pack.name."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="15dp"
android:paddingLeft="15dp"
android:background="@drawable/some_drawable or any selector">

<pack.name..TextViewPlus
    android:id="@+id/menutext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#3D1E00"
    android:textSize="@dimen/TitleNormalM"
    foo:customFont="font.ttf"
    android:gravity="right|center_vertical"
    android:text="blah blah blah"/>
</RelativeLayout>

The problem with your code is that you are using fill_parent as the height and width of individual row items. Instead you should use fill_parent as the width and wrap_content or some dp value as the height as shown in the above code.

I hope this will help :)

Upvotes: 0

Related Questions