1

我一直在处理一段时间,改变collapsingtoolbarlayout中标题背景的颜色,但只有在展开时。为什么我想要么有一个透明纱幕背景或颜色叠加(如文档说):使用网格或颜色覆盖更改CollapsingToolbarLayout中标题的背景颜色

enter image description here

我想这种行为(或透明背景的保护屏幕,也可以),东西我发现,正是这个帖子上的同一个问题:

Android CollapsingToolbarLayout Title background

我申请这个解决我的Android项目,但它没有工作,ImageView的后视图设置不显示(不知道为什么),但我还没有找到的东西(不是在谷歌或在文档中ntation)是如何设法用颜色叠加来实现的。

我想要的是,当工具栏展开,显示标题后面的背景颜色,当它崩溃隐藏起来,只显示标题

这是我到现在为止:

enter image description here

正如您所看到的标题非常难以阅读并取决于后面的imageview,所以我需要找到适合所有可能图像的解决方案。

这是我的代码:

的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> 

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

在此先感谢

+0

这可能有助于https://github.com/anton46/WhatsApp-ProfileCollapsingToolbar。 –

+0

[This](https://stackoverflow.com/a/36616969/3842692)答案工作得很好。 我会推荐它。 – kksal55

+0

[This](https://stackoverflow.com/a/36616969/3842692)答案工作得很好。 我会推荐它。 – kksal55

回答

1

最后我找到了解决方案。为了大家如何处理与CollapsingToolbarLayout和AppBarLayout纱幕的观点,你必须设置最低程序兼容性支持库的23+

的回答了这个问题提供了有效的支持德兴stackoverflow

但请注意设定构建。

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" 
    } 
+0

我们可以在某个地方创建你创建的效果吗? –

+0

Hi @MarianPaździoch,我不明白你的问题..你想看看效果吗? – neteot

+0

是的,我想看看它。 –