2015-11-28 54 views
0

enter image description here更改顏色progmatically無findViewById()

所以我想要做的是..

  1. 獲取繪製形狀(XML文件)
  2. 獲取繪製的顏色
  3. 使用colors.xml中提供的顏色來更改它的顏色
  4. 將該drawable設置爲CardView上的某些ImageView(這不是問題)

這裏是代碼: -

colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="md_red_900">#B71C1C</color> 
    <color name="md_pink_900">#880E4F</color> 
    <color name="md_purple_900">#4A148C</color> 
    <color name="md_deep_purple_900">#311B92</color> 
    ... 
    <!--15 more colors--> 
</resources> 

shape_template.xml(可拉伸形狀): -

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <solid android:color="@color/md_white_1000" /> 
     </shape> 
    </item> 
</selector> 

MainColorPaletteAdapter.java(其中,i要改變顏色繪製的): -

package com.danish.foveros.adapter; 

import android.graphics.drawable.Drawable; 
import android.media.Image; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import com.danish.foveros.R; 
import java.util.ArrayList; 
import java.util.List; 

public class MainColorPaletteAdapter extends RecyclerView.Adapter<MainColorPaletteAdapter.ViewHolder> { 
    List<MainColorPaletteGetSet> mItems; 
    String[] colorName; 
    int[] colorThumbnail; 
    int[] allMaterialColor900; 
    int[] colorNameBackgroundColor; 

    public MainColorPaletteAdapter() { 
     super(); 
     mItems = new ArrayList<MainColorPaletteGetSet>(); 
     MainColorPaletteGetSet color; 

     colorName = new String[]{"Amber", "Brown", "Cyan", "Teal"}; 
     colorThumbnail = new int[]{R.drawable.amber, R.drawable.brown, 
       R.drawable.cyan, R.drawable.teal}; 
     colorNameBackgroundColor = new int[]{R.color.md_amber_900, R.color.md_brown_900, 
       R.color.md_cyan_900, R.color.md_teal_900}; 

     for (int i = 0; i < colorName.length; i++) { 
      color = new MainColorPaletteGetSet(); 
      color.setColorName(colorName[i]); 
      color.setColorThumbnail(colorThumbnail[i]); 
      color.setColorNameBackgroundColor(colorNameBackgroundColor[i]); 
      mItems.add(color); 
     }  
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
     View v = LayoutInflater 
       .from(viewGroup.getContext()) 
       .inflate(R.layout.fragment_home_data, viewGroup, false); 
     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     MainColorPaletteGetSet color = mItems.get(position); 
     holder.colorThumbnail.setImageResource(color.getColorThumbnail()); 
     holder.colorName.setText(color.getColorName()); 
     holder.colorName.setBackgroundColor(color.getColorNameBackgroundColor()); 

    } 

    @Override 
    public int getItemCount() { 
     return mItems.size(); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder { 

     public ImageView colorThumbnail; 
     public TextView colorName; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      colorThumbnail = (ImageView) itemView.findViewById(R.id.colorThumbnail); 
      colorName = (TextView) itemView.findViewById(R.id.colorName); 

     } 
    } 
} 

我試過很多方法,但他們說,以獲得可繪製或通過findViewById()查看的背景顏色,但它不是爲我工作。

正如你可以看到繪製形狀是一個非常簡單的形狀,因此它可能不是很難做到這一點。

我想,而不是創建19個繪製的xml文件,我可以得到一個可繪製的改變它的顏色使用它的第一cardview改變它的顏色,並用它來第二cardview等。

問任何詳細信息,如果您需要。

任何幫助表示讚賞。謝謝。

+0

ImageView.setImageDrawable和Drawable.setColorFilter是你所需要的 – pskink

+0

然後我就試試這個並會讓你知道它是否有效。 –

回答

0

解決它自己: -

package com.danish.foveros.adapter; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.media.Image; 
import android.support.v7.widget.CardView; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.danish.foveros.R; 

import java.util.ArrayList; 
import java.util.List; 

public class MainColorPaletteAdapter extends RecyclerView.Adapter<MainColorPaletteAdapter.ViewHolder> { 
    List<MainColorPaletteGetSet> mItems; 
    String[] colorName; 
    int[] colorThumbnail; 
    int[] allMaterialColor900; 
    int[] colorNameBackgroundColor; 

    public MainColorPaletteAdapter() { 
     super(); 
     mItems = new ArrayList<MainColorPaletteGetSet>(); 
     MainColorPaletteGetSet color; 
     colorNameBackgroundColor = new int[]{ 
       R.color.md_red_900, 
       R.color.md_pink_900, 
       R.color.md_purple_900, 
       R.color.md_deep_purple_900, 
       R.color.md_indigo_900, 
       R.color.md_blue_900, 
       R.color.md_light_blue_900, 
       R.color.md_cyan_900, 
       R.color.md_teal_900, 
       R.color.md_green_900, 
       R.color.md_light_green_900, 
       R.color.md_lime_900, 
       R.color.md_yellow_900, 
       R.color.md_amber_900, 
       R.color.md_orange_900, 
       R.color.md_deep_orange_900, 
       R.color.md_brown_900, 
       R.color.md_grey_900, 
       R.color.md_blue_grey_900 
     }; 

     colorName = new String[]{ 
       "Red", 
       "Pink", 
       "Purple", 
       "Deep Purple", 
       "Indigo", 
       "Blue", 
       "Light Blue", 
       "Cyan", 
       "Teal", 
       "Green", 
       "Light Green", 
       "Lime", 
       "Yellow", 
       "Amber", 
       "Orange", 
       "Deep Orange", 
       "Brown", 
       "Grey", 
       "Blue Grey" 
     }; 
     colorThumbnail = new int[]{ 
       R.color.md_red_500, 
       R.color.md_pink_500, 
       R.color.md_purple_500, 
       R.color.md_deep_purple_500, 
       R.color.md_indigo_500, 
       R.color.md_blue_500, 
       R.color.md_light_blue_500, 
       R.color.md_cyan_500, 
       R.color.md_teal_500, 
       R.color.md_green_500, 
       R.color.md_light_green_500, 
       R.color.md_lime_500, 
       R.color.md_yellow_500, 
       R.color.md_amber_500, 
       R.color.md_orange_500, 
       R.color.md_deep_orange_500, 
       R.color.md_brown_500, 
       R.color.md_grey_500, 
       R.color.md_blue_grey_500 
     }; 


     for (int i = 0; i < colorName.length; i++) { 
      color = new MainColorPaletteGetSet(); 
      color.setColorName(colorName[i]); 
      color.setColorThumbnail(colorThumbnail[i]); 
      color.setColorNameBackgroundColor(colorNameBackgroundColor[i]); 
      mItems.add(color); 
     } 


    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
     View v = LayoutInflater 
       .from(viewGroup.getContext()) 
       .inflate(R.layout.fragment_home_data, viewGroup, false); 
     ViewHolder viewHolder = new ViewHolder(v); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     MainColorPaletteGetSet color = mItems.get(position); 
     holder.colorThumbnailView.setBackgroundResource(color.getColorThumbnail()); 
     holder.colorNameView.setText(color.getColorName()); 
     holder.colorNameView.setBackgroundResource(color.getColorNameBackgroundColor()); 
    } 

    @Override 
    public int getItemCount() { 
     return mItems.size(); 
    } 


    public class ViewHolder extends RecyclerView.ViewHolder { 

     public ImageView colorThumbnailView; 
     public TextView colorNameView; 



     public ViewHolder(final View itemView) { 
      super(itemView); 
      colorThumbnailView = (ImageView) itemView.findViewById(R.id.colorThumbnail); 
      colorNameView = (TextView) itemView.findViewById(R.id.colorName); 
     } 

    } 
} 

最終輸出: -

enter image description here