我的LinearLayout在XML:如何編程與背景顏色和重量增加的LinearLayout到另一個佈局
<LinearLayout
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="@dimen/progress_height"
android:layout_alignParentBottom="true"
android:baselineAligned="false"
android:orientation="horizontal" />
,我想生成動態幾個另一LinearLayouts並把他們的「進步」 equaly間隔,例如:
- 添加的第一個LinearLayout將佔據所有空間。
- 二LL將分享1LL
- 三LL的面積的50%將分享1LL空間的33%和2LL
- 等等...
每個LinearLayout中都會有隨機背景顏色 我寫了這樣的事情:
mProgress = (LinearLayout) findViewById(R.id.progress);
.
.
.
LinearLayout prog = new LinearLayout(this);
prog.setBackgroundColor(CommonUtils.getNextRandomColor());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
prog.setLayoutParams(params);
mProgress.addView(prog);
當用戶按下按鈕,另一LL會產生,具有不同的顏色。
我的方法不起作用。佈局中沒有背景顏色。
也許還有另一種簡單得多的方法來創建某種進度條,並且顏色可以共享一些空間嗎?
我getNextRandomColor方法: 公共靜態INT getNextRandomColor(){ INT [] =顏色新INT [] { R. color.color1, R.color.color2 }; 返回顏色[getNextRandomInt(0,colors.length)]; 我已經添加了「getResources()。getColor「,現在我有一些編譯錯誤。以前,使用這個方法,使用.xml LinearLayouts都可以很好地工作。 – Bresiu
Thanks @Bresiu。我的猜測是正確的,'setBackgroundColor'不指望colorResId像R.color.turquoise ,但是可以通過'getResources()。getColor(R.color.turquoise)'獲得該顏色的真實值。只需嘗試返回'getResources()。getColor(colors [getNextRandomInt(0,colors.length)] );' – ssantos
我編輯了我的第一條評論(不小心碰到輸入太快) – Bresiu