2012-12-18 33 views
0

背景:背景的TextView的多行不正確包裝

我已經得到了包括6個項目奠定了作爲一個網格(2×3)的佈局。所有項目的寬度都是屏幕的一半(每邊減去一點餘量)。每個項目是一個RelativeLayout,其中包含ImageView(背景)和TextView(標籤)。標籤被設置爲包裝文本,並允許其增長到與其位於其上的圖像幾乎一樣寬。之後,它將分成兩行。

問題:

一切看起來都很好,只要文字上一行(見圖片頂部元素)配合。背景很好地包裝了文字。但是,當文本顯示在兩行上時,背景會變得太寬(請參閱圖片中的底部項目)。即使第一行的文本不佔用太多空間,它也會填充最大允許寬度。有沒有辦法讓背景以與僅使用一行的方式相同的方式來包裝文本?

圖片:

screen_capture

佈局XML:

<ImageView 
     android:id="@+id/item_image" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="centerCrop" /> 

    <!-- some stuff I had to remove... --> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:paddingBottom="11.33333dp"> 

     <!-- some other stuff I had to remove... --> 

     <!-- the gray background color is set to the TextView below programmatically together with the text itself. --> 
     <TextView 
      android:id="@+id/item_text_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5.33333dp" 
      android:paddingLeft="7.33333dp" 
      android:paddingRight="20dp" 
      android:paddingTop="1dp" 
      android:paddingBottom="5.33333dp" 
      android:layout_alignParentRight="true" 
      android:gravity="right" /> 
    </RelativeLayout> 
</RelativeLayout> 

回答

0

一種這樣的問題的可能性是textSize

這取決於你如何設置和測量單元使用的是設置你的TextViewtextSize

dp,sp,pt and in可能會導致產生此類問題。

因此請嘗試使用mmpx

而根據我的測試px會給你最好的結果。

編輯:

添加下列屬性的佈局TextView文件。

android:gravity="right|start" 

相反的:

android:gravity="right" 

感謝。

+0

我現在使用的是dp。嘗試與px,但它沒有任何區別。 :/ – TofferJ

+0

我可以在屏幕上看到不同的效果,當我使用'dp'或'pt'時,它會顯示您發佈的問題,但是當我使用'px'時,它可以很好地工作。 –

+0

如果它只是文本寬度的度量問題,我是不是會看到同一個問題的標籤只有一行?只有當它換行到第二行時,寬度纔會保持最大寬度,而不是第一行文本的實際寬度。 – TofferJ