0

I'm using Android Design Library components, and I would like to have an image behind my ToolBar and TabLayout. The image must be cropped and not fit to the size. I'm actually setting my image as android:background="@drawable/AppBarBg" in AppBarLayout which doesn't crop (of course) my image. How can I crop my background image ?

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/AppBarBg"
    android:scaleType="centerCrop"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:orientation="horizontal"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
DevUt
  • 1,280
  • 11
  • 24
Nicolas Hauzaree
  • 743
  • 1
  • 5
  • 9

1 Answers1

0

AppBarLayout is basically just a LinearLayout so setting a background will strech it...

I think that you're looking for CollapsingToolbarLayout... Your layout structure should be like this:

AppBarLayout CollapsingToolbarLayout ImageView (this is the background which you want to centerCrop) ToolBar TabLayout etc...

check it out here: CollapsingToolbarLayout and TabLayout

Community
  • 1
  • 1
MarkySmarky
  • 1,609
  • 14
  • 17
  • thanks I actually tried with this but first I don't want to collapse anything and playing with the different attributes it replaced my image by a color. I will try more with it – Nicolas Hauzaree Jul 13 '15 at 13:27