我试图创建一些非常简单的东西。我的想法是使用带有不同色卡的HorizontalScrollView。
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = (LinearLayout) findViewById(R.id.modeList);
Color[] colorList={Color.BLUE,Color.GREEN,Color.RED,Color.ORANGE,Color.AQUA};
String[] nameList={"Film","Music","Brand","Animals","People"};
for(int i=0; i<5; i++) {
drawCard(parent, nameList[i], colorList[i]);
}
}
这是我用来显示卡片的方法
private void drawCard(LinearLayout parent, String cardTitle, Color color){
int middleColorID, darkColorID;
switch(color){
case RED: {
middleColorID=R.color.middleRed;
darkColorID=R.color.darkRed;
break;
}
case BLUE: {
middleColorID=R.color.middleBlue;
darkColorID=R.color.darkBlue;
break;
}
case ORANGE: {
middleColorID=R.color.middleOrange;
darkColorID=R.color.darkOrange;
break;
}
case PURPLE: {
middleColorID=R.color.middlePurple;
darkColorID=R.color.darkPurple;
break;
}
case CYAN: {
middleColorID=R.color.middleCyan;
darkColorID=R.color.darkCyan;
break;
}
case AQUA: {
middleColorID=R.color.middleAqua;
darkColorID=R.color.darkAqua;
break;
}
case GREEN: {
middleColorID=R.color.middleGreen;
darkColorID=R.color.darkGreen;
break;
}
default: {
middleColorID=R.color.middleGreen;
darkColorID=R.color.darkGreen;
break;
}
}
//create the card and assign background
RelativeLayout child = new RelativeLayout(this);
child.setBackgroundResource(R.drawable.card);
child.setElevation(30);
//set margin and layout parent
RelativeLayout.LayoutParams parentParams = new RelativeLayout.LayoutParams(400, -1);
parentParams.setMargins(10,0,10,0);
parent.addView(child, parentParams);
//create the wave_outline
RelativeLayout waveOutline = new RelativeLayout(this);
Drawable waveDrw = getDrawable(R.drawable.ic_newwave_outline);
waveDrw.setColorFilter(getResources().getColor(darkColorID), PorterDuff.Mode.SRC_ATOP);
waveOutline.setBackground(waveDrw);
RelativeLayout.LayoutParams waveOutlineParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 120);
waveOutlineParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
waveOutlineParams.bottomMargin=120;
child.addView(waveOutline, waveOutlineParams);
//create the wave
RelativeLayout wave = new RelativeLayout(this);
waveDrw = getDrawable(R.drawable.ic_newwave);
waveDrw.setColorFilter(getResources().getColor(middleColorID), PorterDuff.Mode.SRC_ATOP);
wave.setBackground(waveDrw);
RelativeLayout.LayoutParams containerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 120);
containerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
containerParams.bottomMargin=90;
child.addView(wave, containerParams);
//create the bottom filler
RelativeLayout cardFiller = new RelativeLayout(this);
waveDrw = getDrawable(R.drawable.card_bottom);
waveDrw.setColorFilter(getResources().getColor(middleColorID), PorterDuff.Mode.SRC_ATOP);
cardFiller.setBackground(waveDrw);
RelativeLayout.LayoutParams cardFillerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 90);
cardFillerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.addView(cardFiller, cardFillerParams);
//set the name
TextView child_name= new TextView(this);
child_name.setText(cardTitle);
RelativeLayout.LayoutParams child_nameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
child_name.setGravity(Gravity.CENTER);
child_name.setTextSize(30);
child_name.setTextColor(getResources().getColor(R.color.dirtyWhite));
child_nameParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child_nameParams.bottomMargin=30;
child.addView(child_name, child_nameParams);
}
奇怪的是,当我尝试运行此代码时,我使用的两个可绘制波向量在所有卡上都具有相同的颜色(它们似乎获得了传递给该方法的最后一种颜色)。 如果我尝试使用其他xml形状文件而不是矢量来交换资源,则显示的颜色是正确的颜色。这使我认为问题是两个向量,但是我不明白我在想什么。实际上,他们一次成功设置了正确的滤色器,但只有一次成功。似乎旧的筛选器已被覆盖。 有任何想法吗? 谢谢
答案 0 :(得分:1)
指向相同资源的可绘制对象共享它们的状态。这意味着,例如,如果您有2个使用相同资源实例化可绘制对象的视图,则您对一个视图的可绘制对象应用的任何更改也会在另一个视图中看到。
您需要做的就是在getDrawable之后立即调用方法drawable.mutate()。 这样可以确保对一个可绘制实例进行的任何更改对于该特定实例都是唯一的,而不会影响指向相同资源的其他视图中的其他可绘制对象。