2013-01-17 23 views
4

在textview中使用粗體文本時,我有一個奇怪的行爲。如果使用右邊距,某行的Android最後一個字可能會導致不正確的textview尺寸

所以問題如下,我已經創建了一個保存文本的自定義項目,並且應該稍後將其用作列表中的項目,所以該項目被多次包含在scrollviews linearlayout中,該scrolllay具有垂直方向。

項目佈局如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="@style/ButtonItem"> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopLeft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="20dp" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="10dp" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12" /> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopMiddle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" 
     android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft" 
     android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight" 
     android:layout_marginRight="20dp" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12" 
     android:gravity="right"/> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopRight" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="40dp" 
     android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12"/> 
    <TextView 
     android:id="@+id/m_oItem_TextView_Bottom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" 
     android:layout_alignRight="@+id/m_oItem_TextView_TopRight" 
     android:layout_below="@+id/m_oItem_TextView_TopLeft" 
     android:layout_marginTop="5dp" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_14" /> 
    <ImageView 
     android:id="@+id/m_oItem_PlaceHolder" 
     android:layout_width="wrap_content" 
     android:layout_height="10dp" 
     android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" 
     android:layout_alignRight="@+id/m_oItem_TextView_TopRight" 
     android:layout_below="@+id/m_oItem_TextView_Bottom" 
     android:contentDescription="@string/image_description"/> 
    <ImageView 
     android:id="@+id/m_oPart_Arrow_Image" 
     android:scaleType="fitCenter" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:src="@drawable/arrow_right" 
     android:contentDescription="@string/image_description"/> 
</RelativeLayout> 

m_oItem_TextView_Bottom TextView的是將包含文本,可能是因爲它太長了,如果需要,從而增加了TextView的高度的一部分。正如我測試過的,所有工作都正確,textview具有所需的高度。

然而,一旦我設定

android:textStyle="bold" 

當然文本將變得更寬,因此可能需要更多的線路。現在的問題是,這不會被Android以某種方式解決。

例如如果非粗線勉強需要兩個線,如(下圖中最後一個項目)

last item with two lines

中的粗體文字將需要三根線,但TextView的仍然只會提供兩行

last item with three lines

有沒有人遇到過同樣的問題,或有解決方案或至少有一個建議,我怎麼能解決這個問題。由於事先在任何情況下

編輯: 按要求ButtonItem風格是這個

<style name="ButtonItem"> 
    <item name="android:background">@drawable/button_item</item> 
</style> 

@繪製/ button_item之中:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/button_item_down" /> 
    <item android:state_selected="true" android:drawable="@drawable/button_item_selected" /> 
    <item android:drawable="@drawable/button_item_normal" /> 
</selector> 

@繪製/ button_item_down之中:

@ drawable/button_item_selected:

@繪製/ button_item_normal之中:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:bottom="1dp" 
     android:left="-2dp" 
     android:right="-2dp" 
     android:top="-2dp"> 
     <shape android:shape="rectangle" > 
      <solid android:color="@color/transparent" /> 
      <stroke 
       android:width="@dimen/button_item_stroke_thickness" 
       android:color="@color/black" /> 
     </shape> 
    </item> 
</layer-list> 

本質,他們就在那裏創建項目下的黑線,使白色的背景,如果被點擊或選擇。儘管選中的狀態從不使用,因爲選中的狀態從來不會在代碼中設置。

我也試過它沒有風格作爲背景,但如預期的那樣,問題保持不變。

編輯再次: 好吧,所以我能夠追查更多的問題,調整大小的textview其實是正確的。具體問題是如果下面。如果一行的最後一個字(例如,g「這是一個測試」,而測試是最後一個詞)導致換行,最後一個單詞需要足夠長。我使用虛擬字符串對此進行了測試,並在問題中使用了相同的項目。

這裏有一些圖片 enter image description here

正如你可以看到,導致問題需要一個或多個字符,則字足夠長,以適應線

最後編輯時間: 還好找到了答案(見下文)

+0

請提供風格=「@風格/ ButtonItem」 – agamov

回答

0

最後,我找到了這個問題,

因此,這裏的溶液中,該問題是在m_oItem_TextViewBottom的margin_Right其通過引起有效地創造了一個作爲麻煩製造者的保證金權利,我用其他文本瀏覽和正確的利潤率對它進行了測試,它們都有相同的問題。

我用相同邊距的Padding替換它,並以此作爲該項目的最終解決方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    style="@style/ButtonItem"> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopLeft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="20dp" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="10dp" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12" /> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopMiddle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" 
     android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft" 
     android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight" 
     android:layout_marginRight="20dp" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12" 
     android:gravity="right"/> 
    <TextView 
     android:id="@+id/m_oItem_TextView_TopRight" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:paddingRight="40dp" 
     android:layout_alignTop="@+id/m_oItem_TextView_TopLeft" 
     android:textColor="@color/black" 
     android:textSize="@dimen/font_size_12"/> 
    <TextView 
     android:id="@+id/m_oItem_TextView_Bottom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" 
     android:layout_below="@+id/m_oItem_TextView_TopLeft" 
     android:layout_marginTop="5dp" 
     android:paddingRight="40dp" 
     android:textColor="@color/black" 
     android:textStyle="bold" /> 
    <ImageView 
     android:id="@+id/m_oItem_PlaceHolder" 
     android:layout_width="wrap_content" 
     android:layout_height="10dp" 
     android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft" 
     android:layout_alignRight="@+id/m_oItem_TextView_TopRight" 
     android:layout_below="@+id/m_oItem_TextView_Bottom" 
     android:contentDescription="@string/image_description"/> 
    <ImageView 
     android:id="@+id/m_oPart_Arrow_Image" 
     android:scaleType="fitCenter" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="5dp" 
     android:src="@drawable/arrow_right" 
     android:contentDescription="@string/image_description"/> 
</RelativeLayout> 
1

那麼TextView的大小和粗體文本一樣好,所以它的RelativeLayout需要擴展以適應擴展的TextView。使用LinearLayouts(一個垂直,也許另一個水平與您使用RelativeLayout創建的設計相匹配)可能會帶來更多運氣,因爲他們擅長在子級更改時自行調整大小。

參考:

android:layout_alignRight="@+id/m_oItem_TextView_TopRight" 

因此:http://logc.at/2011/10/18/when-to-use-linearlayout-vs-relativelayout/

+0

好,我會給它一個鏡頭,看看細節怎麼說原來 – DokutoMekki

+0

好吧我繼續嘗試有兩個linearlayouts做到這一點的建議,但是這並不改變它,仍然有同樣的問題。我也繼續檢查了29(一行)53(兩行)和77(三行)textview的高度,沒有問題。如果線條被破壞,高度是不同的,但是44(兩條線)和68(三條線)。顯然,textview沒有正確調整大小 – DokutoMekki

相關問題