2016-12-02 56 views
1

我想在CollaspingToolbarLayout内更改ImageView的背景。下面是我试图做的:更改CollapsingToolabarLayout的背景

activity_main.xml中

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:contentScrim="@color/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <ImageView 
      android:id="@+id/collapsingimage" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:background="@drawable/teszt1" 
      app:layout_collapseMode="parallax" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" /> 

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

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    collapsingimage = (ImageView)findViewById(R.id.collapsingimage); 
    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 
    collapsingToolbarLayout.setTitle(" "); 

dynamicToolbarColor(); 

} 

private void dynamicToolbarColor() { 

    Bitmap b = imageUrltoBitmap(imgurl); 
    collapsingimage.setImageBitmap(b); 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.hhg); 

     Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { 

      @Override 
      public void onGenerated(Palette palette) { 
       int colorPrimary = getResources().getColor(R.color.colorPrimary); 
       int colorPrimaryDark = getResources().getColor(R.color.colorPrimaryDark); 
       collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(colorPrimary)); 
       collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(colorPrimary)); 
       collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.colorPrimary)); 
      } 
     }); 
} 

所以我想加载另一个图像,而不是我在xml文件中添加我设置了ImageView的背景,但没有任何反应。你有什么想法如何做到这一点?

回答

0

要更改颜色使用属性app:contentScrim,我看到你正在使用的颜色color/colorPrimary

<android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:contentScrim="@color/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

如果你需要做到这一点编程请勿使用pallete,直接使用:

collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); 

int colorPrimary = getResources().getColor(R.color.colorPrimary); 
collapsingToolbarLayout.setBackgroundColor(colorPrimary); 
collapsingToolbarLayout.setContentScrimColor(colorPrimary); 
collapsingToolbarLayout.setStatusBarScrimColor(colorPrimary); 
+0

感谢您的回答,但您的代码与我的完全相同。可以告诉我,我应该做什么与​​'app:contentScrim'不同? –

+0

你必须改变颜色吗?更改应用程序的颜色:contentScrim = – Jorgesys

+0

我想在运行时更改java代码。我想改变工具栏中ImageView的背景。 –