我想要圓角視圖,並根據運行時的內容更改視圖的顏色。如何以編程方式圍繞角落並設置隨機背景顏色
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>
當然背景顏色和背景圖像相互覆蓋。你想達到什麼目的?什麼是'tags_rounded_corners'? –
你能展示更多代碼嗎?它看起來很好,所以我想知道你可能會使用一種listView,或者重用現有的textview。 – Chansuk
請查看http://www.gadgetsaint.com/tips/rounded-corners-views-layouts-android/#.WPz2QVN97BI – ASP