2016-12-21 70 views
-1

我想改變特定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); 
+0

「textview.findViewById()setBackgroundColor(Color.RED)。」給錯誤。只有當您從XML獲得textview引用時纔可以執行此操作。但是,你正在編程textivew引用,所以你不能這樣做。如果你想設置backgroudn顏色,然後在for循環textview,setBackgroundColor(Color.RED)中這樣做 –

+0

@sohanshetty你是什麼意思。 –

+0

@sohanshetty但是當我點擊按鈕並在ID中傳遞1,所以我只需要改變第一個textview背景顏色休息全部,因爲它是。 –

回答

0

使用此代碼

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); 
     } 
0

試試這個:

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); 
0

使用txtCompanyName.setBackgroundResource(R.color.white);

下面是摘錄可以幫助你在哪裏txtChannelName是TextView的

txtCompanyName.setBackgroundColor(Color.RED)的對象;

txtCompanyName.setBackgroundColor(顏色。parseColor( 「#FFFFFF」));

希望它會幫助你

0

創建類型的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); 
    } 

這將改變你的文本視圖的背景顏色

0

使用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)); 
    } 
相關問題