83

我想要圓角視圖,並根據運行時的內容更改視圖的顏色。如何以編程方式圍繞角落並設置隨機背景顏色

TextView v = new TextView(context); 
v.setText(tagsList.get(i)); 
if(i%2 == 0){ 
    v.setBackgroundColor(Color.RED); 
}else{ 
    v.setBackgroundColor(Color.BLUE); 
} 

v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
v.setPadding(twoDP, twoDP, twoDP, twoDP);    
v.setBackgroundResource(R.drawable.tags_rounded_corners); 

我希望設置一個可繪製的和顏色會重疊,但他們不。無論我執行第二個是由此產生的背景。

有沒有什麼辦法以編程方式創建此視圖,請記住直到運行時纔會決定背景顏色?

編輯:我現在只是在紅色和藍色之間進行交換以進行測試。稍後,顏色將由用戶選擇。

編輯:

tags_rounded_corners.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <corners 
     android:bottomRightRadius="2dp" 
     android:bottomLeftRadius="2dp" 
     android:topLeftRadius="2dp" 
     android:topRightRadius="2dp"/> 
</shape> 
+0

當然背景顏色和背景圖像相互覆蓋。你想達到什麼目的?什麼是'tags_rounded_corners'? –

+0

你能展示更多代碼嗎?它看起來很好,所以我想知道你可能會使用一種listView,或者重用現有的textview。 – Chansuk

+0

請查看http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP

回答

148

代替setBackgroundColor,檢索背景繪製並設置其顏色:

v.setBackgroundResource(R.drawable.tags_rounded_corners); 

GradientDrawable drawable = (GradientDrawable) v.getBackground(); 
if (i % 2 == 0) { 
    drawable.setColor(Color.RED); 
} else { 
    drawable.setColor(Color.BLUE); 
} 

另外,還可以中定義的填充你的tags_rounded_corners.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <corners android:radius="4dp" /> 
    <padding 
    android:top="2dp" 
    android:left="2dp" 
    android:bottom="2dp" 
    android:right="2dp" /> 
</shape> 
+0

Perect answe!它也與strock一起工作,但是我們可以使用類似 的顏色colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); colorDrawable.setColorFilter(color,PorterDuff.Mode.SRC_ATOP); 如果我們沒有邊框。 但在邊框的情況下,你可以讓我知道PorterDuff.Mode,以便筆跡顏色不會改變 –

+0

如何通過XML添加背景顏色? – emaillenin

+2

如果「v」是TextView而不是v.getBackground()會投射「java.lang.ClassCastException:android.graphics.drawable.StateListDrawable不能轉換爲android.graphics.drawable.GradientDrawable」這是真的在'13 ? – sonavolob

86

用於設置圓角並將隨機背景顏色添加到視圖的總體編程方法。 我沒有測試過代碼,但你明白了。

GradientDrawable shape = new GradientDrawable(); 
shape.setCornerRadius(8); 

// add some color 
// You can add your random color generator here 
// and set color 
if (i % 2 == 0) { 
    shape.setColor(Color.RED); 
} else { 
    shape.setColor(Color.BLUE); 
} 

// now find your view and add background to it 
View view = (LinearLayout) findViewById(R.id.my_view); 
view.setBackground(shape); 

這裏我們使用梯度繪製,使我們可以利用GradientDrawable#setCornerRadius因爲ShapeDrawable不提供任何這樣的方法。

+11

shape.setCornerRadii(corners);它非常有用 – umesh

+0

超級回答@jayadeepw –

+7

考慮使用'PaintDrawable'而不是'GradientDrawable'。它支持圓角和只是一種顏色,似乎比漸變更合適。 – Cimlman

2

如果你沒有一個行程,你可以使用

colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); 

colorDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); 

但是這也將改變筆觸顏色

+23

我打算使用這個,但我有中風。 –

7

我覺得這樣做的最快方法是:

GradientDrawable gradientDrawable = new GradientDrawable(
      GradientDrawable.Orientation.TOP_BOTTOM, //set a gradient direction 
      new int[] {0xFF757775,0xFF151515}); //set the color of gradient 
gradientDrawable.setCornerRadius(10f); //set corner radius 

//Apply background to your view 
View view = (RelativeLayout) findViewById(R.id.my_view); 
if(Build.VERSION.SDK_INT>=16) 
    view.setBackground(gradientDrawable); 
else view.setBackgroundDrawable(gradientDrawable);  
0

你可以通過使用如下的DrawableCompat更好地實現它:

Drawable backgroundDrawable = view.getBackground();    
DrawableCompat.setTint(backgroundDrawable, newColor); 
相關問題