0

我可以使用 HTMLCleaner 从网站获取文本。问题是,当我将文本设置为 TextView 时,它会显示文本的开头,上面有很大的空格。截屏

我试过android:gravity了,但什么也没发生。

请帮忙。

这是我的代码:

    private class SiteParser extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... arg) {
        String output = null;

        try {
            HtmlHelper hh = new HtmlHelper(new URL(arg[0]));
            List<TagNode> news = hh.getnewsByClass("TextoPrint");

            for (Iterator<TagNode> iterator = newss.iterator(); iterator
                    .hasNext();) {
                TagNode divElement = (TagNode) iterator.next();
                output = divElement.getText().toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output;
    }

    protected void onPostExecute(String output) {


        Bundle bundle=new Bundle();
        bundle.putString("body",output);
        Intent mainIntent = new Intent(act, MyView.class);
        mainIntent.putExtras(bundle);
                startActivity(mainIntent);
        act.finish();



    }
}

public class HtmlHelper {
    TagNode rootNode;

    public HtmlHelper(URL htmlPage) throws IOException, XPatherException {
        HtmlCleaner cleaner = new HtmlCleaner();
        rootNode = cleaner.clean(htmlPage);

    }

    List<TagNode> getnewsByClass(String Classname){
        List<TagNode> newsList = new ArrayList<TagNode>();


        TagNode divElements[] = rootNode.getElementsByName("div", true);
        for (int i = 0; divElements != null && i < divElements.length; i++) {
            String classType =  divElements[i].getAttributeByName("id");
            if (classType != null && classType.equals(Classname)) {
                newsList.add(divElements[i]);
            }
        }

        return newsList;
    }
}
4

1 回答 1

0

尝试使用以下命令删除任何前导(和尾随)空格trim()

output = divElement.getText().toString().trim();
于 2012-11-12T22:57:56.220 回答