gpt4 book ai didi

java - 如何为每个 GridView 项目赋予不同的背景颜色?

转载 作者:行者123 更新时间:2023-11-30 00:47:02 27 4
gpt4 key购买 nike

我有一个 GridView 作为我的 MainActivity,它包含 11 个带有可点击监听器的项目。每个单元格都由一个 ImageView 和一个 TextView 组成。我想为这 11 个项目中的每一个提供不同的背景颜色。

这是我的项目数组

public static String [] prgmNameList={"TownHall 1","TownHall 2",
"TownHall 3","TownHall 4","TownHall 5 ","TownHall 6",
"TownHall 7","TownHall 8","TownHall 9","TownHall 10","TownHall 11"};
public static int [] prgmImages={R.drawable.town_hall1,
R.drawable.town_hall2,R.drawable.town_hall3,
R.drawable.town_hall4,R.drawable.town_hall5,
R.drawable.town_hall6,R.drawable.town_hall7,
R.drawable.town_hall8,R.drawable.town_hall9,
R.drawable.town_hall10, R.drawable.town_hall11};

这是我的 GridView Adapter 代码

public class CustomAdapterMainActivity extends BaseAdapter {

String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapterMainActivity(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView tv;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
ImageView img;

rowView = inflater.inflate(R.layout.each_item_gridview, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);



rowView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
Class<? extends Activity> activityToStart = null;
switch (position) {

case 0:
activityToStart = TownHallElevenActivity.class;
break;

case 1:
activityToStart = TownHallElevenActivity.class;
break;

case 2:
activityToStart = TownHallElevenActivity.class;
break;

case 3:
activityToStart = TownHallElevenActivity.class;
break;

case 4:
activityToStart = TownHallElevenActivity.class;
break;

case 5:
activityToStart = TownHallElevenActivity.class;
break;

case 6:
activityToStart = TownHallElevenActivity.class;
break;

case 7:
activityToStart = TownHallElevenActivity.class;
break;

case 8:
activityToStart = TownHallNineActivity.class;
break;

case 9:
activityToStart = TownHallTenActivity.class;
break;

case 10:
activityToStart = TownHallElevenActivity.class;

break;
default:
break;

}
Intent i = new Intent(context.getApplicationContext(), activityToStart);
i.putExtra("classFrom", activityToStart.toString());
context.startActivity(i);
}
});

return rowView;
}

}

最佳答案

将此添加到您的 getView() 中:

rowView.setBackgroundColor(colors[position]));

然后像这样添加一组颜色:

public static int [] colors { 0x225522, 0xff00000, .... }

关于java - 如何为每个 GridView 项目赋予不同的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com