请告诉我如何设置textview背景颜色当textview id是动态的
问题描述:
我想改变特定textview
的背景色。每个textview
需要不同的背景。我写这个代码,textview
但这给错误请告诉我如何设置textview背景颜色当textview id是动态的
“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(颜色。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);
}
这将改变你的文本视图的背景颜色
答
使用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());
最好的解决方案
要Genrate浅色使用 -
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
要genrate深色使用 -
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));
}
“textview.findViewById()setBackgroundColor(Color.RED)。”给错误。只有当您从XML获得textview引用时才可以执行此操作。但是,你正在编程textivew引用,所以你不能这样做。如果你想设置backgroudn颜色,然后在for循环textview,setBackgroundColor(Color.RED)中这样做 –
@sohanshetty你是什么意思。 –
@sohanshetty但是当我点击按钮并在ID中传递1,所以我只需要改变第一个textview背景颜色休息全部,因为它是。 –