2016-09-20 65 views
-2

我想創建這樣的標籤:定製一個TextView

enter image description here

的要求是:

  • 矩形應與文本增長
  • 紅線的長度屬性依賴

我試過的是創造e爲文本背景的9ng圖像,並使用填充屬性表示紅線的長度。紅線是通過覆蓋onDraw方法繪製的。

結果是紅線是正確的,但背景展開包括填充像這樣:

enter image description here

什麼是做到這一點的最好方法是什麼?建立一個包含標籤的自定義視圖?以編程方式繪製100%的自定義視圖(在這種情況下,調整9ng圖像的方法是什麼?)?要做到這一點

感謝 朱利安

+0

共享視圖XML代碼。 –

回答

1

試試這個

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="The text" 
      android:textColor="#000000" 
      android:background="@drawable/<your_drawable>" 
      android:textSize="16sp" /> 

      <View 
      android:layout_width="0dp" 
      android:layout_height="1dp" 
      android:background="#f00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

希望它會幫助你

+0

哼,我試着用程序完成它,但這個解決方案很好。我添加了一個包裝類來存儲紅線寬度。有沒有辦法用這種方法繪製點線? – Julien

+0

這是處理它的簡單方法。 –

0

最好的方法可能使用兩個不同的控制器。

您設置了一個簡單的textView,並將矩形作爲背景,寬度設置爲wrap-content

在此之後,您可以根據需要(位於控制器的右側或頁面末尾或您喜歡的位置)創建要放置的位置,然後調整它的大小。

對於任何疑問,我在這裏