简体   繁体   中英

Design Library Android

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM