我有一个声明,我必须添加一条水平线。但它不受android支持,似乎有什么方法可以重新创建
使用支持的标签在 android 中的标签效果。
String message = "Hello <br> hai<br> I am fine <hr>";
tab.setText(Html.fromHtml(message));
它显示你好
,我
很好
但没有水平线。
这里“hr”的 HTML 标记不起作用。有什么方法可以从支持的标签中添加 hr 标签效果。提前致谢..
Html.fromHtml()
目前不支持该<hr>
标签,因此您需要编写自己的标签处理程序来实现Html.TagHandler
. Android 中的 TextView 使用 Span 进行样式设置,因此我们需要创建一个也绘制水平线的 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);
}
}
}
小时跨度.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 宽度属性更改为 match_parent。
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 或任何您喜欢的标签
Android 不支持 hr 标签。您将不得不为带有背景颜色的水平线添加一个单独的视图作为您的文本。
试试这个水平线
View line = new View(this);
line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));
line.setBackgroundColor(Color.rgb(51, 51, 51));
layout.addView(line);
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>
您添加一个高度为 1 或 2、宽度设置为匹配父级的视图,并指定背景颜色。但这意味着您将拥有 3 个文本视图。
这是来自工作代码。如您所见,任何子视图都可以:
<!-- divider 1 -->
<LinearLayout
android:id="@+id/lineA"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#000000" />
另一种选择是使用绝对支持 <hr> 的 WebView,但这很可能是矫枉过正。