2013-03-15 54 views
0

我有一個應用程序的問題,並希望顯示一個進度條,以顯示剩餘/回答多少個問題。 如果問題的答案正確,我希望將進度條的顏色設爲綠色,如果答案錯誤,我會設置爲紅色。兩種顏色的進度條

假設有5個問題。在例如3個問題,進度條應該是

green|red|red|grey|grey 

如果問題1是正確的,2和3是錯誤的...

+1

一個進度條沒有按」噸似乎是最好的匹配這 – njzk2 2013-03-15 16:26:12

回答

1

我發現了一個工作解決方案..可能不是最好的,所以評論appriciated!

在我XML的文件

<TextView 
     android:id="@+id/info" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/info" 
     /> 

    <LinearLayout 
     android:id="@+id/take_test_progress_bar_linear_layout" 
     android:layout_width="match_parent" 
     android:layout_height="10dp" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:layout_below="@+id/info" 
     android:orientation="horizontal" > 
    </LinearLayout> 

    <TextView 
     android:id="@+id/question" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/take_test_progress_bar_linear_layout" 
     android:text="@string/stt1" 
     /> 

在我活動

oncreate(){ 
.... 

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
for(int k=0; k<mTotalQuestionNum; k++) 
{ 
    View v = new View(this); 
    v.setBackgroundColor(Color.GRAY); 

    params.weight = (float)1.0/((float) mTotalQuestionNum); 
    v.setLayoutParams(params); 
    mProgressLayout.addView(v,k); 

} 
... 
} 

然後,操作時的答案...

if(correct) 
    mProgressLayout.getChildAt(mQuestionNum-1).setBackgroundColor(Color.GREEN); 
else 
    mProgressLayout.getChildAt(mQuestionNum-1).setBackgroundColor(Color.RED); 
+0

道歉,我整個週末都不在電腦上 - 很高興你解決了你的問題。我唯一需要注意的是你的「重量」計算是不必要的,只需將它們的「重量」設置爲「1」即可。 – Rawkode 2013-03-18 10:51:33

0

由於njzk2在評論中說,一個ProgressBar不是爲正確的組件工作。我想建議使用LinearLayout

您可以編程方式添加儘可能多的Views裏面,因爲你需要,使用權重相等的水平分佈,爲用戶前進通過你的問題,你可以改變這些Views的背景顏色。

+0

好吧,我會試試看...我的標籤可能有點混亂,因爲它仍然是一個進度條,但不是一個Android'進度條'... – EirikO 2013-03-15 16:34:49

+0

如果您在執行時遇到問題,我會爲你一起提供一些示例代碼。祝你好運 – Rawkode 2013-03-15 16:38:55

+0

一些幫助將不勝感激,@Rawkode! :) – EirikO 2013-03-16 09:24:45