我想更改特定textview
的背景颜色。每个textview
都需要不同的背景。我编写此代码,textview
进行,但这给出了错误
" textview.findviewbyid()。setbackgroundColor(color.red);"
Textview textview;
for (int i = 0; i < 3; i++) {
textview = new TextView(this);
textview .setId(i);
textview .setTextSize(15);
textview .setTextColor(Color.BLACK);
}
textview.findViewById(1).setBackgroundColor(Color.RED);
自我解决方案
使用线性布局并添加 textview
然后更改
LinearLayout linear_layout;
Textview textview;
for (int i = 0; i < 3; i++) {
textview = new TextView(this);
textview .setId(i);
textview .setTextSize(15);
textview .setTextColor(Color.BLACK);
linear_layout.add(textview);
}
linear_layout.findViewById(1).setBackgroundColor(Color.RED);
使用此代码
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
Random rand = new Random();
for (int i = 0; i < 3; i++) {
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int randomColor = Color.rgb(r,g,b);
Textview textview = new TextView(this);
textview.setId(i);
textview.setTextSize(15);
textview.setTextColor(Color.BLACK);
textview.setBackgroundColor(randomColor);
linearLayout.addView(textview);
}
尝试以下:
List<TextView> textViewList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
TextView textview = new TextView(this);
textview.setTextSize(15);
textview.setTextColor(Color.BLACK);
textViewList.add(textview);
}
int index = 2;
textViewList.get(index).setBackgroundColor(Color.RED);
使用 txtcompanyname.setBackgroundResource(r.color.white);
或
下面的摘要可能会帮助您在txtchannelname是textView的对象
的对象txtcompanyname.setBackgroundColor(color.red);
或
txtcompanyname.setBackgroundColor(color.parsecolor("#ffffff"));
希望它能帮助您
创建类型textView的列表,然后添加您的textView,如下
List<TextView> textViewList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
TextView textview = new TextView(this);
textview.setTextSize(15);
textview.setTextColor(Color.BLACK);
textViewList.add(textview);
}
然后,按钮单击"传递您的值"以更改文本背景颜色如下
private void changeBackGroundColor(int index) {
textViewList.get(index).setBackgroundColor(Color.RED);
}
这将改变您各自的文本查看bacground color
使用getrandomcolor方法
public int getRandomColor(){
Random rnd = new Random();
return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
}
textViewList.get(index).setBackgroundColor(getRandomColor());
最佳解决方案
染色浅色使用此 -
public int generateRandomLightColor() {
// This is the base color which will be mixed with the generated one
final int baseColor = Color.WHITE;
final int baseRed = Color.red(baseColor);
final int baseGreen = Color.green(baseColor);
final int baseBlue = Color.blue(baseColor);
final int red = (baseRed + mRandom.nextInt(256)) / 2;
final int green = (baseGreen + mRandom.nextInt(256)) / 2;
final int blue = (baseBlue + mRandom.nextInt(256)) / 2;
return Color.rgb(red, green, blue);
}
它显示颜色,例如-https://stackoverflow.com/a/43235/4741746
为了生成深色,使用此 -
public int getRandomDarkColor(){
Random rnd = new Random();
//use rnd.nextInt(0x1000000) & 0x7F7F7F
return Color.argb(255, rnd.nextInt(0x1000000), rnd.nextInt(0x1000000), rnd.nextInt(0x1000000));
}