4

我正在改進我的應用程序的用戶界面。 而在我使用的設計中,我有一個TextView,它在某些時候會充當進度條。 的ruslt應該是這樣的:TextView作爲帶textcolor minipulation的進度條?

enter image description here

的事情是文本的一部分將改變他們的顏色,而進步正在改變

我看着在android系統spannablestring它會工作,如果我能找到由進步覆蓋(但我不認爲這將是輕易/準確地)的字母

什麼現在我打算待辦事項是使用以下命令:

  • FrameLayout with:
  • 背景textView與綠色文本和白色背景。
  • 前面Linearlayout改變寬度進展。
  • 帶有與framelayout寬度匹配的綠色背景和白色文本的TextView。

有沒有更好的方法?

回答

8

更好的方法會要求你重寫TextView類。您可以使用剪裁來分割TextView並以不同的顏色繪製兩個部分。

TextView tv = new TextView(this){ 
    @Override 
    public void draw(Canvas canvas) { 
     int color1 = Color.WHITE; 
     int color2 = Color.GREEN; 

     canvas.save(); 
     setTextColor(color1); 
     setBackgroundColor(color2); 
     canvas.clipRect(new Rect(0, 0, (int)(getWidth() * percent), getHeight())); 
     super.draw(canvas); 
     canvas.restore(); 

     canvas.save(); 
     setTextColor(color2); 
     setBackgroundColor(color1); 
     canvas.clipRect(new Rect((int)(getWidth() * percent), 0, getWidth(), getHeight())); 
     super.draw(canvas); 
     canvas.restore(); 
    } 
}; 

我希望你明白了吧

+0

只需添加canvas.restore();每次繪製之後,代碼準備好去 – 2013-02-20 13:11:02

+0

yup,我沒有時間去測試它:)謝謝 – Zielony 2013-02-20 13:48:48

+0

由於某些原因,當我嘗試上面的代碼時,文本顏色成功更改,但背景仍然爲colour1 – Buli1212 2017-12-04 23:25:18

1

最簡單的辦法是使用進度和一個TextView上相對佈局。 你提供你自己的風格,這就是它。當ProgressBar擁有您所需的全部內容時,嘗試在TextView上執行它毫無意義。您的佈局看起來就像是:

<RelativeLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ProgressBar android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true"/> 

     <TextView android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Doing something" 
      android:gravity="center" 
      android:layout_centerVertical="true"/>   
</RelativeLayout> 

你只需要按照你希望他們:)

1

與寬度和高度玩,我知道這是一個老問題,但我張貼這一點,因爲它可能會幫助有人正在尋找類似的情況,希望這會幫助有人通過這個現在。這裏有一個例子

1 2 3

我的解決方案主要集中在畫布上用drawText繪製。這裏的技巧是你畫的兩個文本,一個酒吧範圍內,一個視圖邊界內,並使用下面的Z-索引順序(按照示例中使用):

  • 文本(白色)
  • 紅酒吧
  • 文本(黑色)
  • 觀察背景

我就如何實現這樣一個例子,在這個樣本/ lib下我使用自定義的ImageView做到這一點。隨意使用它,因爲無論如何你喜歡它:https://github.com/danilodanicomendes/InvertedTextProgressBar