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
@ramaral, it’s the same :\
– porthfind
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
– rsicarelli
@sicachester but when using Imageview how do you set the image as background?
– porthfind