Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add padding for background image

Tags:

android

I have a LinearLayout which has a background image (a 9 patched png file). How can I add padding to left and right so that the background image does not take up the whole width? I have tried android:paddingLeft and android:paddingRight, but that does not change anything.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:paddingLeft="25dip"     android:paddingRight="25dip"     android:background="@drawable/background"> 

The whole background still stretches the whole screen width.

like image 546
michael Avatar asked Apr 26 '10 05:04

michael


People also ask

Can you add padding to an image?

In the File Properties dialog, select the Appearance tab. In the Padding section, enter numbers in the Left, Right, Top, and/or Bottom fields to set the width of the padding (in pixels). Click OK. The padding is added to the image, displayed in the default background color of the image.

Can you add padding to IMG tag?

Selective Padding However, you can add padding selectively by using padding-bottom, padding-left, padding-right, and padding-top. Here are a few examples: style="padding-left: 10px; padding-bottom: 20px;"

Does padding affect background image?

The padding-box value means that the only the padding box part of the HTML element has the background image rendered. The content-box value means that the only the content box part of the HTML element has the background image rendered.


1 Answers

You should use xml drawable, specially designed for this purposes - Inset Drawable: http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset

For example:

res/drawable/inset_background.xml:

<?xml version="1.0" encoding="utf-8"?> <inset xmlns:android="http://schemas.android.com/apk/res/android"     android:drawable="@drawable/your_background_image"     android:insetRight="25dip"     android:insetLeft="25dip" /> 

and then in your xml layout:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:background="@drawable/inset_background"> 
like image 129
Vasily Sochinsky Avatar answered Sep 22 '22 12:09

Vasily Sochinsky