带有HTML内容的TextView的额外填充 [英] Extra padding on TextView with HTML contents

查看:124
本文介绍了带有HTML内容的TextView的额外填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 TextView

 < TextView 
android:id =@ + id / issue_journal_item_notes
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_alignLeft =@ + id / issue_journal_item_details
android:layout_below =@ + id / issue_journal_item_details
android:background =@ drawable / journal_item_notes_background
android:padding =8dp
android:text =issue_journal_item_notes
android:textIsSelectable =true/>

我正在填写以下内容:

<$< br />你认为在崩溃期间你可以得到一个logcat吗?这看起来很奇怪,尤其是它使得Android您可以在这里获得SDK:http://developer.android.com/sdk/index.html<br/>(需要logcat)< / p>;
theTextView.setText(Html.fromHtml(html));

结果如下:





Assignee ...是另一个 TextView

我的 TextView 是灰色背景。它的边界清晰可见,非常浅灰色。左边较深的灰色条是背景的一部分,所以它也是 TextView



我们可以清楚请参阅8dp方形填充。然而,底部的空白处是什么?这是一种填充,但我没有用XML或代码设置它!



如果有人问,我需要HTML支持,因为与上面所示的截图不同,内容可以具有一些HTML内容(< pre> < i> < b> 等)。 你看到的padding实际上只是一个换行符,然后是另一个换行符:

Html.fromHtml(...)时, / code>实现,你会遇到以下处理段落标签的方法:

  private static void handleP SpannableStringBuilder text){
int len = text.length(); (len> = 1&& text.charAt(len-1)=='\\\
'){
if(len> = 2&& amp; ; text.charAt(len - 2)=='\\\
'){
return;
}

text.append(\\\
);
return;
}

if(len!= 0){
text.append(\\\
\\\
);


$ / code>

上面的代码片段来自 Android 4.2.2来源。逻辑非常简单,基本上可以确保每个段落标记都以 \\\
\\\
结尾,以在两个元素块之间给出一个可视间隔。这意味着框架不会考虑整个Html文本是仅包含一个段落(您的情况)还是多个连续的paragaps - 在转换结束时,总是是两个换行符这就是说,如果你知道你总是在处理一个段落,最简单的解决方案是在将它包含在 Html.fromHtml(...)。这是几乎所有其他答案中提出的建议。



现在,由于您提到这不是真正的选择,因此可以选择trim Html.fromHtml(...)的结果,删除任何尾随的空格。 Android会返回一个 Spanned (通常这是一个 SpannableStringBuilder 对象),不幸的是,它不会带有内置-in trim()方法。然而,想出自己的想法并不是太棘手,或者借用其中一个可用的实现。

trim的基本实现)方法会像这样:

  public static CharSequence trim(CharSequence s,int start, int start){
while(start< end&& Character.isWhitespace(s.charAt(start))){
start ++;


while(end> start&& Character.isWhitespace(s.charAt(end - 1))){
end--;
}

return s.subSequence(start,end);
}

要使用它,请将原始代码更改为:

  String html =< p>你好,< br />你认为在崩溃期间你可以得到一个logcat吗? ,尤其是它让Android重启。< br />你可以在这里获得SDK:http://developer.android.com/sdk/index.html<br/>(需要logcat)< br /> / p>中; 
CharSequence trimmed = trim(Html.fromHtml(html));
theTextView.setText(trimmed);

和voilà,之前和之后:


I have this TextView:

<TextView
    android:id="@+id/issue_journal_item_notes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/issue_journal_item_details"
    android:layout_below="@+id/issue_journal_item_details"
    android:background="@drawable/journal_item_notes_background"
    android:padding="8dp"
    android:text="issue_journal_item_notes"
    android:textIsSelectable="true" />

I'm filling this with:

String html = "<p>Hi,<br/>Do you think you could get a logcat during the crash? That seems really strange, especially the fact that it makes Android reboot.<br/>You can get the SDK here: http://developer.android.com/sdk/index.html<br/>(needed for logcat)</p>";
theTextView.setText(Html.fromHtml(html));

This results in:

"Assignee ..." is another TextView.
My TextView is the one with the grey background. Its bounds are clearly seen with the very light grey. The left darker gray bar to the left is part of the background, so it's also the TextView

We can clearly see the 8dp square padding. However, what's the empty space at the bottom? It's some kind of padding, but I havent set this in XML nor in code!

In case somebody asks, I need HTML support, because unlike in the screenshot shown above, the contents may have some HTML content (<pre>, <i>, <b>, etc).

解决方案

The extra 'padding' you're seeing, is in fact just a line break followed by another line break:

When you dive into the Html.fromHtml(...) implementation, you'll come across the following method that handles paragraph tags:

private static void handleP(SpannableStringBuilder text) {
    int len = text.length();

    if (len >= 1 && text.charAt(len - 1) == '\n') {
        if (len >= 2 && text.charAt(len - 2) == '\n') {
            return;
        }

        text.append("\n");
        return;
    }

    if (len != 0) {
        text.append("\n\n");
    }
}

Above snippet was takes from the Android 4.2.2 source. The logic is quite straightforward and basically ensures that every paragraph tag ends with \n\n, to give a visual gap between two element blocks. It means the framework will not into account whether the whole Html text consists of only a single paragraph (your case), or multiple, successive paragaps - there will always be two line breaks at the end of a transformed paragraph.

That being said, if you know you're always dealing with a single paragraph, the easiest solution is to remove that wrapping paragraph before feeding it to Html.fromHtml(...). This is pretty much what was proposed in one of the other answers.

Now, since you mentioned this isn't really an option, the alternative would be to 'trim' the result of Html.fromHtml(...), removing any trailing white spaces. Android returns a Spanned (usually this is a SpannableStringBuilder object), which, unfortunately, doesn't come with a built-in trim() method. It's not too tricky to come up with your own though, or borrow one of several implementations available out there.

A basic implementation for a trim() method would like somewhat like this:

public static CharSequence trim(CharSequence s, int start, int end) {
    while (start < end && Character.isWhitespace(s.charAt(start))) {
        start++;
    }

    while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
        end--;
    }

    return s.subSequence(start, end);
}

To use it, change your original code to:

String html = "<p>Hi,<br/>Do you think you could get a logcat during the crash? That seems really strange, especially the fact that it makes Android reboot.<br/>You can get the SDK here: http://developer.android.com/sdk/index.html<br/>(needed for logcat)</p>";
CharSequence trimmed = trim(Html.fromHtml(html));
theTextView.setText(trimmed);

And voilà, before and after:

这篇关于带有HTML内容的TextView的额外填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆