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

是的,我想看看它。 –