我将我的customadapter中每行的背景颜色设置为
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupon_list_row, null);
int temp_index = ExtraFu.randInt(0, 9);
convertView.setBackgroundColor(color_arr[temp_index]);
Log.d("temp_index", String.valueOf(temp_index));
}
我的颜色数组
int color_arr[]={R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
这是函数randInt
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
行背景设置为两种颜色。这个随机数是一直生成的吗?
答案 0 :(得分:3)
用于随机颜色生成:
var spawn = require('child_process').spawn,
var stream = require('stream');
cmd = spawn('cmd', ['/s', '/c', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln'], { stdio: [process.stdin, process.stdout, 'pipe'] });
var customStream = new stream.Writable();
customStream._write = function (data, ...argv) {
console.log('your notation');
process.stderr._write(data, ...argv);
};
cmd.stderr.pipe(customStream);
在检索项目位置后设置每个项目的private int getRandomColor() {
SecureRandom rgen = new SecureRandom();
return Color.HSVToColor(150, new float[]{
rgen.nextInt(359), 1, 1
});
}
:
BackGroundColor
答案 1 :(得分:2)
if (position%4 == 0){
// set convertView Background
} else if (position%4 == 1){
// set convertView Background
} else if (position%4 == 2){
// set convertView Background
} else if (position%4 == 3){
// set convertView Background
}
这将在列表视图中随机生成四种不同的颜色。
内部适配器
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = lv.inflate(res, null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (position%4 == 0){
holder.textView.setBackgroundColor(Color.parseColor("#1e86cf"));
} else if (position%4 == 1){
holder.textView.setBackgroundColor(Color.parseColor("#2ca0ea"));
} else if (position%4 == 2){
holder.textView.setBackgroundColor(Color.parseColor("#2cc4ea"));
} else if (position%4 == 3){
holder.textView.setBackgroundColor(Color.parseColor("#2ceae3"));
}
return convertView;
}
ViewHolder类:
class ViewHolder{
public TextView textView;
}
适配器自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"/>
答案 2 :(得分:2)
我已修改您的代码
你需要使用下面的行我已经检查了它的工作finr我的布局
Fragment
我未更新的更新代码
convertView.setBackgroundResource(color_arr[rnd]);
参考代码是我尝试过并且工作正常所以需要进行必要的更改
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupon_list_row, null);
int color_arr[]= {R.color.cred,R.color.cpink,R.color.cpurple,R.color.cdpurple,R.color.cindigo,R.color.cblue,R.color.cdorange,R.color.cgreen,R.color.cbroun,R.color.ccyan};
int rnd = new Random().nextInt(color_arr.length);
//convertView.setBackgroundColor(color_arr[temp_index]);
convertView.setBackgroundResource(color_arr[rnd]);
}
答案 3 :(得分:0)
使用以下代码获取随机颜色
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
convertView.setBackgroundColor(color);