Center background image

1

I'm trying to put as an image of the background an image that I want it to be exactly centered vertically and horizontally (the image is a ball). So the goal is to stay horizontally match_parent and vertically wrap_content to stay right in the center of the screen.

I'm not getting this effect because I'm also using the Toolbar and when I get the image well Toolbar also comes to the center. Can someone help me please?

Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:layout_gravity="center"
    android:orientation="vertical"
    android:background="@drawable/ball"
    tools:context=".MainActivity">


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/grey0">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/white"
            android:textSize="26sp"
            android:textStyle="bold"
            android:gravity="center"
            android:id="@+id/toolbar_title"/>

    </android.support.v7.widget.Toolbar>


    <LinearLayout

        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"> (...) </LinearLayout> </LinearLayout>
    
asked by anonymous 25.04.2015 / 23:28

1 answer

1

My suggestion is to organize your View's into "layers", having ImageView in the background, with Drawable that has the image you want to center. By doing this you can centralize a certain "layer" without interfering with the alignment of other View's .

Using FrameLayout would be:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- Primeira "camada", com o ImageView que vai ficar centrado ao fundo -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ball" />

    <!-- Segunda camada com o conteudo a frente do ImageView -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/grey0">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColor="@color/white"
                android:textSize="26sp"
                android:textStyle="bold"
                android:gravity="center"
                android:id="@+id/toolbar_title"/>

        </android.support.v7.widget.Toolbar>

        <LinearLayout
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- Restante das views -->

        </LinearLayout>
    </LinearLayout>
</FrameLayout>

To better understand FrameLayout , I recommend a visit to this question: How to leave imageView behind the buttons? .

    
26.04.2015 / 00:15