Center background image

Asked

Viewed 3,710 times

1

I am trying to put as background image an image that I want 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 make it look like the center of the screen.

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

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>
  • Try taking out the attribute android:layout_gravity of Toolbar.

  • @ramaral, it’s the same :\

  • Hello, take a look at item 2) of this answer and see if it helps you! The concept is the same http://answall.com/questions/59276/imagem-de-background-distortida-no-android-studio/59336#59336

  • @sicachester but when using Imageview how do you set the image as background?

1 answer

1


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

Using the 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 the FrameLayout, recommend a visit to this question: How to let imageView behind the buttons?.

  • Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.