android中的<hr />标签用法

时间:2013-10-18 08:24:22

标签: android html tags textview

我有一个声明,我必须加上一条水平线。但它没有得到Android的支持似乎有任何方法可以使用支持的标签在Android中重新创建


标签效果。

String message = "Hello <br> hai<br> I am fine <hr>";
tab.setText(Html.fromHtml(message));

它表明 你好

我很好

但没有水平线。

这里是&#34; hr&#34;的HTML标记。不管用。有没有办法从支持的标签添加hr标签效果。提前谢谢..

6 个答案:

答案 0 :(得分:8)

Html.fromHtml()目前不支持<hr>标记,因此您需要编写自己的标记处理程序来实现Html.TagHandler。 Android中的TextViews使用Spans进行样式设置,因此我们需要创建一个绘制水平线的Span,我们称之为HrSpan

爪哇

String html = "Hello <br> hai<br> I am fine <hr> And here's another line";

HtmlTagHandler tagHandler = new HtmlTagHandler();
Spanned styledText = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY, null, tagHandler);
textView.setText(styledText);

HtmlTagHandler.java

public class HtmlTagHandler implements Html.TagHandler {

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (tag.equals("hr")) {
            handleHrTag(opening, output);
        }
    }

    private void handleHrTag(boolean opening, Editable output) {
        final String placeholder = "\n-\n";
        if (opening) {
            output.insert(output.length(), placeholder);
        } else {
            output.setSpan(new HrSpan2(), output.length() - placeholder.length(), output.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

HrSpan.java

public class HrSpan extends ReplacementSpan {

    @Override
    public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, @Nullable Paint.FontMetricsInt fm) {
        return 0;
    }

    @Override
    public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8); // 8px tall line

        int middle = (top + bottom) / 2;
        // Draw a line across the middle of the canvas
        canvas.drawLine(0, middle, canvas.getWidth(), middle, paint);
    }
}

科特林

val html = "Hello <br> hai<br> I am fine <hr> Another line here <hr><hr>"
val tagHandler = HtmlTagHandler()

textView.text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY, null, tagHandler)
class HtmlTagHandler : Html.TagHandler {

    override fun handleTag(opening: Boolean, tag: String?, output: Editable?, xmlReader: XMLReader?) {
        if (output == null) return

        when (tag) {
            "hr" -> handleHrTag(opening, output)
            // Handle other tags if needed
        }
    }

    private fun handleHrTag(opening: Boolean, output: Editable) {
        val placeholder = "\n-\n" // Makes sure the HR is drawn on a new line
        if (opening) {
            output.insert(output.length, placeholder)
        } else {
            output.setSpan(HrSpan(), output.length - placeholder.length, output.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
    }
}
class HrSpan : ReplacementSpan() {

    override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: Paint.FontMetricsInt?) = 0

    override fun draw(
        canvas: Canvas, text: CharSequence?, start: Int, end: Int, x: Float, top: Int, y: Int,
        bottom: Int, paint: Paint
    ) {
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 8f

        // Draw line in the middle of the available space
        val middle = ((top + bottom) / 2).toFloat()

        canvas.drawLine(0f, middle, canvas.width.toFloat(), middle, paint)
    }
}

这会给你一个像这样的结果。它匹配TextView的宽度,因此如果您希望线占据整个宽度,请将TextView width属性更改为match_parent。

答案 1 :(得分:2)

Android Html.fromHTML不支持所有代码。

如果你想要这样的标签,我建议你使用WebView

WebView webview = new WebView(this);

String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>";

webview.loadData(summary, "text/html", "utf-8");

WebView将允许您拥有HR或任何您喜欢的标签

答案 2 :(得分:1)

Android中不支持

hr标记。您必须为水平线添加单独的视图,背景颜色为文本。

答案 3 :(得分:1)

尝试使用水平线

View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.rgb(51, 51, 51));
layout.addView(line);

答案 4 :(得分:0)

添加高度为1或2的视图,宽度设置为与父级匹配,并指定背景颜色。 但这意味着你将有3个文本视图。

这是来自工作代码。如您所见,任何视角的孩子都会这样做:

        <!-- divider 1 -->
        <LinearLayout
            android:id="@+id/lineA"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#000000" />

另一种选择是使用一个肯定支持&lt; hr&gt;的WebView,但这很可能是一种过度杀伤。

答案 5 :(得分:0)

Android不支持

hr标记, 如果你想要行

,我的建议是这样的

在活动中:

TextView tt = (TextView)findViewById(R.id.textView1);
LinearLayout LL = (LinearLayout)findViewById(R.id.ll1);

String message = "Hello <br> hai<br> I am fine <hr>";
tt.setText(Html.fromHtml(message));

View hLine = new View(this);
hLine .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
hLine .setBackgroundColor(Color.WHITE);
LL.addView(hLine );

在Xml中:

 <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textView1"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>