Change background color of title in CollapsingToolbarLayout using scrim or color overlay

孤街浪徒 提交于 2020-01-03 02:00:29

问题


I have been dealing for a while now with changing the color of the background of the title in the collapsingtoolbarlayout but only when is expanded. Why I want is either have a transparent scrim background or a color overlay (like the documentation says):

I want this behaviour (or a protective screen with transparent background is also possible), the thing is I found exactly the same question on this post:

Android CollapsingToolbarLayout Title background

I've applied this solution to my android project but it didn't work, the view set after the imageView is not displaying (don't know why) but what I haven't found yet (not in google nor in the documentation) is how to manage to do this with a color overlay.

What I want is that when the toolbar is expanded to show a color background behind the title and when it collapse hide it and only show the title

This is what I have until now:

As you see the title is very difficult to read and depends on the imageview that is behind, so I need to find a solution that fits all the possible images .

This is my code:

the EstablecimientosDetail file:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = getIntent();
        establecimiento = (HashMap<String, String>) i.getBundleExtra(TAG_ESTABLECIMIENTOS).getSerializable("Hashmap");

        setContentView(R.layout.activity_establecimientos_detalle);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_establecimiento_detalle);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
        toolBarLayout.setTitle(establecimiento.get(TAG_NOMBRE));
        toolBarLayout.setContentScrimColor(R.color.black_semi_transparent);
//        toolBarLayout.setCollapsedTitleTextColor(R.color.abc_primary_text_material_light);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Llamando...", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        loadImage();
    }

    private void loadImage() {
        final ImageView imageView = (ImageView) findViewById(R.id.backdrop);
        Log.e("imagen actividad", establecimiento.get(TAG_IMAGEN_RECETA));
        Picasso.with(this).load(establecimiento.get(TAG_IMAGEN_RECETA)).error(R.drawable.logo_ternera_negro).into(imageView);
    }

The corresponding layout file:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/Theme.TerneraGallega.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <!--<View-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="?attr/actionBarSize"-->
            <!--android:background="#000"-->
            <!--android:layout_gravity="bottom"/>-->

        <!--<View-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="@dimen/sheet_text_scrim_height_top"-->
            <!--android:background="@drawable/scrim_top"-->
            <!--app:layout_collapseMode="pin" />-->

        <View
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="bottom"
            android:background="@drawable/scrim_bottom" />

        <!--<include layout="@layout/toolbar" />-->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_establecimiento_detalle"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/EstablecimientosDetalleTheme" />

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

<include layout="@layout/content_establecimientos_detalle" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_action_call"
    app:borderWidth="0dp"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end" />

And also the styles and drawable files:

scrim_botton.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:startColor="@color/translucent_scrim_bottom"
        android:centerColor="@color/translucent_scrim_bottom_center"
        android:endColor="@android:color/transparent"/>
</shape>

styles.xml

    <style name="EstablecimientosDetalleTheme" parent="Theme.AppCompat.NoActionBar">

    <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>

     <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item>

    <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>

    <item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>

</style>

Does anyone know how to achieve this behaviour?

Thanks in advance


回答1:


Finally I have found the solution. To everyone how is dealing with scrim views with CollapsingToolbarLayout and AppBarLayout you have to set the minimum appcompat support library an support desing of 23+

The answered provided in this question is valid stackoverflow

But please be aware of setting the build.grade

compile 'com.android.support:design:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.0'

Along with

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.irixgalicia.terneragallega"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }


来源:https://stackoverflow.com/questions/33541905/change-background-color-of-title-in-collapsingtoolbarlayout-using-scrim-or-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!