在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以某種方式解決。
例如如果非粗線勉強需要兩個線,如(下圖中最後一個項目)
中的粗體文字將需要三根線,但TextView的仍然只會提供兩行
有沒有人遇到過同樣的問題,或有解決方案或至少有一個建議,我怎麼能解決這個問題。由於事先在任何情況下
編輯: 按要求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「這是一個測試」,而測試是最後一個詞)導致換行,最後一個單詞需要足夠長。我使用虛擬字符串對此進行了測試,並在問題中使用了相同的項目。
這裏有一些圖片
正如你可以看到,導致問題需要一個或多個字符,則字足夠長,以適應線
最後編輯時間: 還好找到了答案(見下文)
請提供風格=「@風格/ ButtonItem」 – agamov