2014-06-12 38 views
0

在string.xml中,我有一個包含多個項目的字符串數組。在TextView之間顯示圖像

< string-array name="content"> 
< item> 
< ![CDATA[text line 1 
testing testing testing 
testing testing testing]]> 
< /item> 

我在TextView中顯示該項目使用下面的代碼片段中:

String content = getResources().getStringArray(R.array.content)[i]; 
text = (TextView)rootView.findViewById(R.id.textView1); 
text.setText(Html.fromHtml(content)); 

現在,我想在這個文本之間插入圖片。例如「文本行1」之後的一個圖像和「測試測試」之後的另一個圖像。 我該如何實現此功能?

--Sushant

回答

1

您可以藉助ImageGetter類實現相同的任務。 試試下面的代碼: -

public class TestImageGetter extends Activity implements ImageGetter { 
    private final static String TAG = "TestImageGetter"; 
    private TextView mTv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test_image_getter); 
     String source = "this is a test of <b>ImageGetter</b> it contains " + 
       "two images: <br/>" + 
       "<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" + 
       "<img src=\"http://developer.android.com/assets/images/icon_search.png\">"; 

     Spanned spanned = Html.fromHtml(source, this, null); 
     mTv = (TextView) findViewById(R.id.text); 
     mTv.setText(spanned); 
    } 

    @Override 
    public Drawable getDrawable(String source) { 
     LevelListDrawable d = new LevelListDrawable(); 
     Drawable empty = getResources().getDrawable(R.drawable.ic_launcher); 
     d.addLevel(0, 0, empty); 
     d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight()); 

     new LoadImage().execute(source, d); 

     return d; 
    } 

    class LoadImage extends AsyncTask<Object, Void, Bitmap> { 

     private LevelListDrawable mDrawable; 

     @Override 
     protected Bitmap doInBackground(Object... params) { 
      String source = (String) params[0]; 
      mDrawable = (LevelListDrawable) params[1]; 
      Log.d(TAG, "doInBackground " + source); 
      try { 
       InputStream is = new URL(source).openStream(); 
       return BitmapFactory.decodeStream(is); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      Log.d(TAG, "onPostExecute drawable " + mDrawable); 
      Log.d(TAG, "onPostExecute bitmap " + bitmap); 
      if (bitmap != null) { 
       BitmapDrawable d = new BitmapDrawable(bitmap); 
       mDrawable.addLevel(1, 1, d); 
       mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
       mDrawable.setLevel(1); 
       // i don't know yet a better way to refresh TextView 
       // mTv.invalidate() doesn't work as expected 
       CharSequence t = mTv.getText(); 
       mTv.setText(t); 
      } 
     } 
    } 
} 

見下面的鏈接,我可以從以下鏈接幫助實現相同的任務: -

Html.ImageGetter TextView

+0

有什麼辦法來處理的strings.xml文件這一功能? – user1767570

相關問題