2016-09-27 100 views
2

我有一個TextView填充文本應該包含一些ImageSpan對象。該圖像可以是比正常線高度導致以下問題更高:TextView與ImageSpan混淆線高

  • 如果圖像是一個行的最後一個對象,以下行的高度是正確
  • 如果最後一個對象是的圖像,以下行的高度設定爲含有圖像行的高度

這裏是正確的情況: enter image description here


這是錯誤的情況: enter image description here

更有趣的是,如果有一個在文本換行字符,行高爲再次從這一點不錯。

TextView只是一個非常基本的一個:

<TextView 
    android:id="@+id/text_02" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="18dp" 
    android:text="Text 02" /> 

(該TextView坐落在一個LinearLayout這是一個ScrollView

這是我如何創建跨區文字:

TextView textView02 = (TextView) findViewById(R.id.text_02); 

SpannableString string = new SpannableString(LOREM_IPSUM); 
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 102, 103, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 105, 106, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
string.setSpan(new ImageSpan(this, R.mipmap.ic_launcher), 108, 109, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 

textView02.setText(string); 

有沒有人有任何關於溶膠的想法爲此嗎?我寧願不實現TextView的線條繪製方法...

+0

你有沒有想過這個 – j2emanue

+0

不是reall。但是,我用多個模擬器圖像測試了它,並且在我看來,只有API 23(6.0)才能生成此圖像。另外,如果我在API 23手機上運行它(我認爲是6.0.2),它永遠不會顯示這種行爲。我認爲開發人員在原始的API 23中搞砸了一些東西,但稍後將其修復。 –

回答

0

嘗試將高度設置爲要與ImageSpan一起顯示的繪圖。比如像這樣:

Drawable vegetary = mContext.getResources().getDrawable(R.drawable.ic_best_veget); 
    vegetary.setBounds(0, 0, vegetary.getIntrinsicWidth(), <yourHeight>); 
    ssb.setSpan(new ImageSpan(vegetary, ImageSpan.ALIGN_BASELINE), ssb.length()-1, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);