2012-11-08 52 views
2

我有一個用戶在窗體中回答問題的項目。 我需要實現一個水平進度條(不是對話框),用點代替普通的條。Android - 帶點的水平進度條

當用戶回答問題時,該特定問題的點會改變顏色。 這可能嗎?我正在使用API​​ 16(目標)和API 7(最低)與Sherlock ActionBar

+0

試試這個https://stackoverflow.com/a/37741873/7104450 –

回答

1

如果您沒有大量問題,如果您只是使用多個ImageView來表示點就可能就足夠了。 XML的例子:

<RelativeLayout 
     android:id="@+id/dotsL" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/dot1" 
      android:layout_width="5dp" 
      android:layout_height="5dp" 
      android:layout_marginTop="5dp" 
      android:background="@drawable/circle_white" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/dot2" 
      android:layout_width="5dp" 
      android:layout_height="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/dot1" 
      android:background="@drawable/circle_white" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/dot3" 
      android:layout_width="5dp" 
      android:layout_height="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/dot2" 
      android:background="@drawable/circle_white" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/dot4" 
      android:layout_width="5dp" 
      android:layout_height="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/dot3" 
      android:background="@drawable/circle_white" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/dot5" 
      android:layout_width="5dp" 
      android:layout_height="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/dot4" 
      android:background="@drawable/circle_white" > 
     </ImageView> 
    </RelativeLayout> 

在java中:

mDots = new ImageView[5]; 
    mDots[0] = (ImageView) rootView.findViewById(R.id.dot1); 
    mDots[1] = (ImageView) rootView.findViewById(R.id.dot2); 
    mDots[2] = (ImageView) rootView.findViewById(R.id.dot3); 
    mDots[3] = (ImageView) rootView.findViewById(R.id.dot4); 
    mDots[4] = (ImageView) rootView.findViewById(R.id.dot5); 

當用戶回答一個問題,剛纔:

mDots[questionNumber].setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_green)); 

注意:此方法要求你必須根據可繪製。

+0

我認爲這將工作,如果我已經知道用戶會回答的問題的數量,但問題的數量不在我的控制,用戶在網站上創建表單,他可以創建一個10個問題或100個表單。我不能在xml中做一個靜態數量的點,我必須用N因子以編程方式實例化他們。 – Zephyros

+0

你是對的情況下,您必須以編程方式實例化ImageViews,這當然是可能的。也許不是解決這個問題最乾淨的方式,但它應該工作。 – slezadav

1

XML:

<LinearLayout 
    android:id="@+id/image_layout" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"> 

動態地添加圖片和隱藏他們(基於問題總數),我也把layout_weight等於1,你可以看到,所以每個「點」具有相同的大小:每個答案後

ImageView[] images = new ImageView[totQuestions]; 
LinearLayout imageLayout = (LinearLayout) findViewById(R.id.image_layout); 
ImageView image; 
LinearLayout.LayoutParams layoutParams; 
for(int i = 0; i < totQuestions; i++){ 
    image = new ImageView(this); 
    layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1.0f); 
    image.setLayoutParams(layoutParams); 
    image.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle_green)); 
    image.setVisibility(View.INVISIBLE); 
    images[i] = image; 
    imageLayout.addView(image); 
} 

顯示圖片:

imageLayout.getChildAt(questionNr).setVisibility(View.VISIBLE); //starting at 0 

我只是改進的地方slezadav建議。

+0

我會測試這個,明天我會發佈一個反饋,tkz! – Zephyros

+0

雖然我還沒有測試過,但我很肯定它的工作原理。這可能是一些小問題。 – Carnal

0

Android框架已經包含一個ProgressBar小部件。這適用於所有類型的佈局,並不限於對話框。使用它並指定android:progressDrawable或android:indeterminateDrawable屬性並繪製自己的點將是最簡單的解決方案。詳情請參閱documentation