在没有findViewById()的情况下逐步更改可绘制xml文件的颜色

时间:2015-11-28 18:41:29

标签: android colors drawable

enter image description here

所以我想做的是......

  1. 获取可绘制的形状(xml文件)
  2. 获取可绘制的颜色
  3. 使用colors.xml中可用的颜色更改颜色
  4. 将该可绘制设置为CardView上的某个ImageView(这不是问题)
  5. 以下是代码: -

    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(我想要改变drawable的颜色): -

    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()得到View的drawable或background颜色,但它对我不起作用。

    正如您所看到的那样,可绘制的形状是一个非常简单的形状,因此可能并不难。

    我想要创建19个可绘制的xml文件,我可以得到一个可绘制的更改,它的颜色用于第一次cardview更改它的颜色并将其用于第二次cardview等等。

    如果需要,请提供更多信息。

    感谢任何帮助。谢谢。

1 个答案:

答案 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