使用稀松布或颜色叠加更改 CollapsingToolbarLayout 中标题的背景颜色

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

我一直在处理更改折叠工具栏布局中标题背景的颜色,但只有在展开时才这样做。我想要的原因是要么有透明的稀松布背景,要么有颜色覆盖(如文档所述):

我想要这种行为(或者透明背景的保护屏也是可以的),问题是我在这个post上发现了完全相同的问题:

我已经将此解决方案应用到我的 android 项目中,但它没有用,在 imageView 之后设置的视图不显示(不知道为什么),但我还没有找到(不在 google 中,也不在文档中)是如何使用颜色叠加来做到这一点。

我想要的是当工具栏展开时在标题后面显示彩色背景,当它折叠时隐藏它并只显示标题

这是我目前所拥有的:

如您所见,标题很难阅读并且取决于后面的图像视图,所以我需要找到适合所有可能图像的解决方案。

这是我的代码:

EstablecimientosDetail 文件:

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);
    }

对应的布局文件:

<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" />

还有样式和可绘制文件:

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>

有谁知道如何实现这种行为?

提前致谢

终于找到解决办法了。对于每个人如何使用 CollapsingToolbarLayout 和 AppBarLayout 处理稀松布视图,您必须设置最小的 appcompat 支持库和 23+

的支持设计

本题提供的答案有效

但请注意设置 build.grade

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

一起
android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

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