Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - xml layout as a background

Is it possible to make a xml background composed of multiple image / scale / alignement? And so to have a complex drawable xml usable as a background for android layout.

For example, this is the dynamic xml background that I would like to do: enter image description here

And so, for all of my different activities, put my different layouts/views above: enter image description here

I know I cannot set a layout as a layout_background, but here is the design of what I would like to do (according to previous pictures):

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/main_image" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/footer_image" />
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

I saw we can set bitmap to drawable xml, but for scale and alignement? Is there an easier way to make this visual rendering?

like image 306
Bibix Avatar asked Mar 19 '13 13:03

Bibix


People also ask

How do I add a background to my layout?

in your parent layout element, e.g. linearlayout or whatever, simply add android:background="@drawable/background" This will set the background of your layout, assuming you have the image in a /drawable folder.

What is Android XML layout?

You can declare a layout in two ways: Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. You can also use Android Studio's Layout Editor to build your XML layout using a drag-and-drop interface.

Which is the default layout for XML in Android Studio?

The Constraint layout is the default layout used in Android.

What is include in Android XML?

In Android, for reusing the complete layouts we can use the <include/> and <merge/> tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements.


1 Answers

You can create one Layout as you want in dynamic xml background and can Include in all screens behind RelativeLayout like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/my_dynamic_layout"/>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

    ...Your layout code ...

    </RelativeLayout >

</RelativeLayout >
like image 121
Chirag Patel Avatar answered Sep 17 '22 23:09

Chirag Patel