2010-07-04 72 views

回答

91

發現以下的,似乎做的工作:

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
      TextView textView = (TextView) findViewById(R.id.textview); 
      SpannableString ss = new SpannableString("abc"); 
      Drawable d = getResources().getDrawable(R.drawable.icon32); 
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
      ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
      ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
      textView.setText(ss); 
} 
+2

'd.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());'是可繪製的關鍵不具有固有界限。 – 2015-10-27 21:17:35

+0

getDrawable函數折舊請更新您的答案 – 2017-03-17 10:23:22

+1

如果我想調整ImageSpan在可擴展的 .. 字符串中的位置?例子我想給底部的Imagpan一個10dp的餘量。 – Khay 2017-05-04 07:24:14

17

SpannableString + ImageSpan不要在Android的API 21 & 22工作(我在Android Studio中1.2測試。 1.1模擬器),但如果你這樣做:

TextView textView = (TextView) findViewById(R.id.textview); 
textView.setTransformationMethod(null); 
... 
textView.setText(ss); 

SpannableString + ImageSpan將工作。

我被這個帖子的啓發:https://stackoverflow.com/a/26959656/3706042

+2

我的設備運行API'22'顯示'SpannableString' +'ImageSpan'很好。我使用'VectorDrawable'雖然... – Sakiboy 2017-05-19 07:50:58

+0

你確實是正確的+1!這種神奇的方法使它工作 – Corbella 2017-12-14 06:11:36