动态获取图像并将其放置在Android中的正确位置

时间:2015-11-12 19:04:17

标签: android html xml image layout

当我通过XML解析从url获取数据时,我得到了html字符串。现在在这个html字符串中。有一个多图像标签。我从这个html字符串中获取了所有src =“”。

现在我的问题是我不知道有多少图像会出现以及将会有什么位置。我将获取图像,但问题是如何在正确的位置显示这些图像。我使用文本视图

这是我的文本视图布局,我必须在正确的位置显示图像(它们位于html字符串中)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.talha.appforblog.ColoumnView"
>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ColoumnView" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:maxLines = "1000"

    android:scrollbars = "vertical"/>

</RelativeLayout>

这里我得到了src标签

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    try {
            urlBlogtitle = mainBlogPost.get(position).title; // getting title of the clicked item
            //Log.d(TAG, urlBlogtitle);
            summarygetter = databaseChecker(urlBlogtitle);
            Log.d(TAG, summarygetter);
        Pattern p = Pattern.compile("src\\s*=\\s*['\"]([^'\"]+)['\"]");
        Matcher m = p.matcher(summarygetter);
            while (m.find()) {
                try {

                    String srcResult = m.group(1);
                Log.d("yahan immage ka src a rha", m.group(1));
            }
                catch(Exception e)
                {
                    Log.d("test","exceptiojn iin img");
                }

            }
            if (summarygetter == "false") {
                new DownloadXmlTaskContent(getActivity()).execute(URL);
            } else {
                Log.d(TAG, "ab database se uthaya");
                startingIntent(summarygetter);

            }

            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            //mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);

    }
    catch(Exception e){
        Log.d("test",""+e);
    }
}

这是我的html字符串

So we are doing it again. I know what you are thinking, that everybody keeps writing these articles what’s the difference. Well I had the same confusion when I started. So I came up with idea of one interesting fact about each university. Let’s just hope I can keep this up.
<a href="http://rarelyknownthings.com/wp-content/uploads/2015/10/graduation_hat.png"><img class="alignnone size-full wp-image-288" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/graduation_hat.png" alt="graduation_hat" width="620" height="443" /></a>
This article will state world’s top 10 engineering universities. The criteria is set on the basis of research done.
<!--nextpage-->
<h3><strong>10 : Harvard University</strong></h3>
In the 10<sup>th</sup> position comes Harvard. Now some of you might think that’s not right, but apparently it is. I’m following QS format and Harvard comes at the 10<sup>th</sup> position in engineering universities. Established in 1636 its one of the most prestigious universities of the world. Not to mention United States oldest higher learning university.
<img class="aligncenter wp-image-209 size-full" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/harward.png" alt="harvard" width="975" height="488" />
The interesting fact about Harvard is the Harvard Library. It was established 2 years later in 1638 and holds more than 18 million volumes. Not to mention the library system consists of 73 libraries. (Beat this figure!) In the overall world ranking it comes at 2<sup>nd</sup> position.
&nbsp;
<!--nextpage-->
<h3><strong>9 : Oxford University
</strong></h3>
There’s no clear date of Oxford’s establishment. But evidence point to 1096 so we’ll go with that. Now the main era begin after the establishment of Cambridge University. I’ll go into that later. The university holds 38 colleges and is mostly self-govern tutorial based. The famous thing about the university is its scholarships from being 100 years old like Rhodes scholarship, to some recent.
<img class="aligncenter wp-image-218 size-full" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/oxford.png" alt="oxford" width="975" height="609" />
The most interesting fact about the university is that it holds world’s largest university press. (I don’t even know what they do with such a big press) In the world overall ranking Oxford comes at 6<sup>th</sup> position.
<!--nextpage-->
<h3><strong>8 : University of California, Berkeley (UCB)</strong></h3>
Berkeley was established in 1868. It was a merger between different colleges of California. It’s not very old as compared to the others but they made their name in a very short time. The university’s pride is its award winning alumni and the research section where they spend millions of dollars for this part. There are 72 Nobel Prizes, nine Wolf Prizes, seven Fields Medals and many more included in this list.
<img class="aligncenter size-full wp-image-221" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/ucb.png" alt="ucb" width="975" height="543" />
The university’s interesting fact is that these guys love finding things. In this attempt they found not only 6 different periodic table’s element but also along with Berkeley 16 chemical elements that’s the highest number for any university. Berkeley is ranked 26<sup>th</sup> in the overall world ranking.
<!--nextpage-->
<h3><strong>7 : Imperial College London</strong></h3>
The Imperial College doesn’t really have a great start like the other. It was established 1907. At that time it was only a technology and engineering university. Later on some of the medical schools attached to it making its way to biomedical. Right now there are four faculties in total. There are a lot of big names like Nicholas Tombazis (McLaren and Ferrari Chief Designer) and Alexander Fleming coming from here.
<img class="aligncenter wp-image-213 size-full" src="http://rarelyknownthings.com/wp-content/uploads/2015/10/imperial2.png" alt="imperial2" width="975" height="731"

1 个答案:

答案 0 :(得分:0)

你无法在textview中呈现html标签,你可以做的是,放置一个webview而不是textview并在其上加载html字符串。

举个例子,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/yourwebview" />

</RelativeLayout>

然后,(基于你的html字符串)创建完整格式化的html字符串,如下所示,

String formatedString = "<!DOCTYPE html>" + "<html>" + "<body>" + <your html string> + "</body>" +"</html>";

然后,

yourWebview.loadDataWithBaseURL(null, formatedString, "text/html",
                "UTF-8", null);