Domanda

How do i set background image in Linear Layout so that it could fit every devices and what is the resolution for that image?

È stato utile?

Soluzione

Use this link for supporting different screen sizes. You will have to make same image with different resolutions

And to set the image as background,

usage:

android:background:"@drawable/background"

Altri suggerimenti

Take this repeatable drawable app_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/your_image"
        android:tileMode="repeat" />

And use it like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_background"/>

And if you want to scale your_image to all screen resolutions, define it as a 9-patch image: http://developer.android.com/tools/help/draw9patch.html

If you want to set through xml, then you need to do as below:

android:background="@android:color/white"

in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use

android:background="@colors/white"

If you want to do programmatically, then do:

linearlayout.setBackgroundColor(Color.WHITE);

and if you want to set an image

android:background="@drawable/image"

You can keep your image in res/drawable folders or create a folder inside res main folder which is name drawable-nodpi which is support all dpi device. and also you can follow Simple Nine-patch Generator

then set background image thus. such as

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher">

Best of luck!

For using a background from drawable folder,

android:background="@drawable/background" >

prepare 4 images and place them in their respective folders.

xhdpi (xlarge screens) 960dp x 720dp

hdpi (large screens) 640dp x 480dp

mdpi (normal screens) 470dp x 320dp

ldpi (small screens) 426dp x 320dp

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top