2016-03-15 53 views
0

我有一個場景,我有一個String其中有HTML標記。在html標記中,可以有多個或一個img標記。現在,我必須顯示嵌入文本中的圖像。爲此,我使用了textview,我遵循了這個偉大的answer。但它沒有給我需要的結果。所以,後來我搜索了網絡&碰到CompoundView如何製作複合視圖

注意僅限於希望使用web視圖。

我已看過這些教程。

  1. tutplus
  2. vogella
  3. Ryanharter的博客post

但我沒有得到從哪裏開始以及如何開始使用了。因爲我不知道字符串中會有多少圖片標籤。

如果你們任何人指導我如何去做,那將是非常好的。一些準則將不勝感激。

在此先感謝!

+0

如果它是你想顯示的HTML內容然後使用[WebView] – pskink

+0

我不想使用webview –

+0

你是什麼意思通過''我不要'「?這是用於這個的視圖,你想自己寫嗎? – pskink

回答

-1

因此,經過幾個小時的挖掘,我發現這裏的一些解決方案是一些細節的代碼。

此答案可能有幫助。我用Jsoup從字符串中提取<Img/>標記,然後我在Textview中顯示ImageView<p>中的圖像。結果是根據我需要的。另外我用通用圖像加載器Libaray加載圖像ImageView然後我添加視圖以編程方式佈局在我的情況佈局是linearlayout,所以我做了一個輔助類,並通過內容,HTML字符串和線性佈局作爲參數。

在您的項目中添加jsoup。

compile 'org.jsoup:jsoup:1.9.2' 

這裏有一些片段。

public class PostContentHandler { 
Context context; 
String content; 
LinearLayout linearLayout; 

public PostContentHandler(Context context, String content, LinearLayout linearLayout) { 
    this.context = context; 
    this.content = content; 
    this.linearLayout = linearLayout; 

} 

public void setContentToView() { 

    //custom font 
    Typeface bitterBoldFont = Typeface.createFromAsset(context.getAssets(), "fonts/Bitter-Regular.otf"); 

    List<String> p = new ArrayList<>(); 
    List<String> src = new ArrayList<>(); 
    List<String> li = new ArrayList<>(); 
    Document doc = Jsoup.parse(content); 

    Elements elements = doc.getAllElements(); 

    for (Element element : elements) { 
     Tag tag = element.tag(); 
     if (tag.getName().matches("h[1-6]{1}")) { 
      String heading = element.select(tag.getName().toString()).text(); 
      TextView textView = new TextView(context); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams(); 
      int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top); 
      int start = (int) context.getResources().getDimension(R.dimen.content_margin); 
      int end = (int) context.getResources().getDimension(R.dimen.content_margin); 
      params.setMargins(start, top, end, 0); 
      textView.setTextSize(20); 
      textView.setTypeface(bitterBoldFont); 
      textView.setText(heading); 
      textView.setTextColor(context.getResources().getColor(R.color.black)); 
      linearLayout.addView(textView); 
     } 
if (tag.getName().equalsIgnoreCase("p")) { 
      element.select("img").remove(); 
      String body = element.html(); 
      TextView textView = new TextView(context); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams(); 
      int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top); 
      int start = (int) context.getResources().getDimension(R.dimen.content_margin); 
      int end = (int) context.getResources().getDimension(R.dimen.content_margin); 
      params.setMargins(start, top, end, 0); 
      textView.setTypeface(bitterBoldFont); 
      textView.setLinksClickable(true); 
      textView.setAutoLinkMask(Linkify.WEB_URLS); 
      textView.setText(Html.fromHtml(body)); 
      textView.setTextColor(context.getResources().getColor(R.color.content_color)); 
      linearLayout.addView(textView); 
      p.add(body); 
     } 
     if (tag.getName().equalsIgnoreCase("ol")) { 
      String ol = element.select(tag.getName().toString()).outerHtml(); 
      TextView textView = new TextView(context); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams(); 
      params.setMarginStart((int) context.getResources().getDimension(R.dimen.content_margin)); 
      int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top); 
      int start = (int) context.getResources().getDimension(R.dimen.content_margin); 
      int end = (int) context.getResources().getDimension(R.dimen.content_margin); 
      params.setMargins(start, top, end, 0); 
      textView.setTypeface(bitterBoldFont); 
      textView.setLinksClickable(true); 
      textView.setAutoLinkMask(Linkify.WEB_URLS); 
      textView.setTextColor(context.getResources().getColor(R.color.content_color)); 
      textView.setText(Html.fromHtml(ol, null, new MyTagHandler())); 
      linearLayout.addView(textView); 

     } 
     if (tag.getName().equalsIgnoreCase("ul")) { 
      String ul = element.select(tag.getName().toString()).outerHtml(); 
      TextView textView = new TextView(context); 

      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams(); 
      int top = (int) context.getResources().getDimension(R.dimen.heading_margin_top); 
      int start = (int) context.getResources().getDimension(R.dimen.content_margin); 
      int end = (int) context.getResources().getDimension(R.dimen.content_margin); 
      params.setMargins(start, top, end, 0); 
      textView.setTypeface(bitterBoldFont); 
      textView.setLinksClickable(true); 
      textView.setAutoLinkMask(Linkify.WEB_URLS); 
      textView.setTextColor(context.getResources().getColor(R.color.content_color)); 
      textView.setText(Html.fromHtml(ul, null, new MyTagHandler())); 
      linearLayout.addView(textView); 
     } 
     if (tag.getName().equalsIgnoreCase("img")) { 
      String url = element.select("img").attr("src"); 
      final ImageView imageView = new ImageView(context); 
      imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      final ProgressBar progressBar = new ProgressBar(context); 
      linearLayout.addView(progressBar); 
      progressBar.setVisibility(View.GONE); 
      ImageLoader imageLoader = ImageLoader.getInstance(); 
      imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() { 
       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        super.onLoadingComplete(imageUri, view, loadedImage); 
        progressBar.setVisibility(View.INVISIBLE); 
        int height = loadedImage.getHeight(); 
        imageView.getLayoutParams().height = getScreenWidth(); 
        imageView.setAdjustViewBounds(true); 
        imageView.requestLayout(); 
       } 

       @Override 
       public void onLoadingStarted(String imageUri, View view) { 
        super.onLoadingStarted(imageUri, view); 
        progressBar.setVisibility(View.VISIBLE); 
       } 
      }); 
      linearLayout.addView(imageView); 
      src.add(url); 
     } 

    } 
} 

public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 
} 

我希望我的回答能幫助別人。