如何在Android中的json中显示textview中的图像和文本

时间:2016-05-02 06:27:54

标签: android json

我想为一个网站开发Android应用程序,我希望从json阅读网站帖子,然后在RecyclerView中显示。
我希望将展示图片(<img>)加入TextView!我的意思是在TextView中显示文本和图像 JSON代码:

"status": "ok",
"count": 10,
"count_total": 28,
"pages": 3,
"posts": [{
    "id": 145,
    "type": "post",
    "slug": "english-post-2",
    "url": "http:\/\/tellfa.com\/tafrihgah\/?p=145",
    "status": "publish",
    "title": "English Post",
    "title_plain": "English Post",
    "content": "<p>This post is test for show Text and Image<\/p>\n<p><img class=\"alignnone size-medium wp-image-79\" src=\"http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-300x240.jpg\" alt=\"[WallpapersMania]_vol49-027\" width=\"300\" height=\"240\" srcset=\"http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-300x240.jpg 300w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-768x614.jpg 768w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-1024x819.jpg 1024w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027.jpg 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>This is image in Text<\/p>\n",
    "excerpt": "<p>This post is test for show Text and Image<\/p>\n<p>This is image in Text<\/p>\n",
    "date": "2016-05-01 08:20:39",
    "modified": "2016-05-01 08:20:39",
    "categories": [{
        "id": 1,
        "slug": "%d8%af%d8%b3%d8%aa%d9%87%e2%80%8c%d8%a8%d9%86%d8%af%db%8c-%d9%86%d8%b4%d8%af%d9%87",
        "title": "\u062f\u0633\u062a\u0647\u200c\u0628\u0646\u062f\u06cc \u0646\u0634\u062f\u0647",
        "description": "",
        "parent": 0,
        "post_count": 24
    }],
    "tags": [],
    "author": {
        "id": 1,
        "slug": "tellfa",
        "name": "\u0645\u062d\u0645\u062f",
        "first_name": "",
        "last_name": "",
        "nickname": "\u0645\u062d\u0645\u062f",
        "url": "http:\/\/codesaz.com",
        "description": "\u0627\u06cc\u0646 \u0632\u0646\u062f\u06af\u06cc \u0646\u0627\u0645\u0647 \u0645\u0646 \u0627\u0633\u062a",
        "avatar": "76"
    },
    "comments": [],
    "attachments": [{
        "id": 146,
        "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031.jpg",
        "slug": "wallpapersmania_vol33-031",
        "title": "WallpapersMania_vol33-031",
        "description": "",
        "caption": "",
        "parent": 145,
        "mime_type": "image\/jpeg",
        "images": {
            "full": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031.jpg",
                "width": 1600,
                "height": 1200
            },
            "thumbnail": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-150x150.jpg",
                "width": 150,
                "height": 150
            },
            "medium": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-300x225.jpg",
                "width": 300,
                "height": 225
            },
            "mediaphase-frontpage-news": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-300x220.jpg",
                "width": 300,
                "height": 220
            },
            "mediaphase-blog-large": {
                "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-700x313.jpg",
                "width": 700,
                "height": 313
            }
        }
    }],
    "comment_count": 0,
    "comment_status": "open",
    "thumbnail": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-150x150.jpg",
    "custom_fields": {},
    "thumbnail_size": "thumbnail",
    "thumbnail_images": {
        "full": {
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031.jpg",
            "width": 1600,
            "height": 1200
        },
        "thumbnail": {
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-150x150.jpg",
            "width": 150,
            "height": 150
        },
        "medium": {
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-300x225.jpg",
            "width": 300,
            "height": 225
        },
        "mediaphase-frontpage-news": {
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-300x220.jpg",
            "width": 300,
            "height": 220
        },
        "mediaphase-blog-large": {
            "url": "http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/05\/WallpapersMania_vol33-031-700x313.jpg",
            "width": 700,
            "height": 313
        }
    }
}

显示文字和图片的Json代码:

"content": "<p>This post is test for show Text and Image<\/p>\n<p><img class=\"alignnone size-medium wp-image-79\" src=\"http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-300x240.jpg\" alt=\"[WallpapersMania]_vol49-027\" width=\"300\" height=\"240\" srcset=\"http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-300x240.jpg 300w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-768x614.jpg 768w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027-1024x819.jpg 1024w, http:\/\/tellfa.com\/tafrihgah\/wp-content\/uploads\/2016\/04\/WallpapersMania_vol49-027.jpg 1280w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>This is image in Text<\/p>\n",

活动代码:

public class PostShow_page extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

    private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR = 0.9f;
    private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS = 0.3f;
    private static final int ALPHA_ANIMATIONS_DURATION = 200;

    private boolean mIsTheTitleVisible = false;
    private boolean mIsTheTitleContainerVisible = true;

    private LinearLayout mTitleContainer;
    private TextView mTitle;
    private AppBarLayout mAppBarLayout;
    private Toolbar mToolbar;

    private TextView postShow_title, postShow_title2, postShow_content;
    private ImageView post_cover;

    private String title = "", image = "", content = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_show_page);

        bindActivity();

        mAppBarLayout.addOnOffsetChangedListener(this);

        mToolbar.inflateMenu(R.menu.post_menu);
        startAlphaAnimation(mTitle, 0, View.INVISIBLE);

        post_cover = (ImageView) findViewById(R.id.postShow_cover_image);
        postShow_title = (TextView) findViewById(R.id.postShow_title);
        postShow_title2 = (TextView) findViewById(R.id.postShow_titleBig);
        postShow_content = (TextView) findViewById(R.id.postShow_content_text);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            title = bundle.getString("title");
            image = bundle.getString("image");
            content = bundle.getString("content");
        }
        if (title != null) {
            postShow_title.setText(title);
            postShow_title2.setText(title);
        }
        if (image != null) {
            Glide.with(this)
                    .load(image)
                    .placeholder(R.drawable.post_image)
                    .into(post_cover);
        }
        if (content != null) {
            postShow_content.setText(Html.fromHtml(content));
        }

    }

我希望在postShow_content中显示图片和文字,我该怎么做?

注意:请不要给我负面评价,我在谷歌搜索但没有找到我的问题的答案。我很业余,我真的需要你的帮助!谢谢所有&lt; 3

2 个答案:

答案 0 :(得分:0)

请查看这个,

 Drawable myDrawable = getResources().getDrawable(R.drawable.ic_launcher);
 Bitmap bm      = ((BitmapDrawable) myDrawable).getBitmap();
 Drawable d = new BitmapDrawable(bm);
 d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
 postShow_content.setCompoundDrawables(null, d, null, null);

这可能会对你有所帮助。

答案 1 :(得分:0)

使用WebView: 例如:webView.loadData(contentHere,&#34; text / html&#34;,&#34; UTF-8&#34;); 它将显示图像和文本