gpt4 book ai didi

android - 如何在android textview中进行文本对齐并同时使用自定义字体样式?

转载 作者:行者123 更新时间:2023-11-30 02:35:02 24 4
gpt4 key购买 nike

public class FontTextView extends TextView {

public FontTextView(Context context) {
super(context);
Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
this.setTypeface(face);
}

public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
this.setTypeface(face);
}

public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
this.setTypeface(face);
}

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}

}

我有一个像这样的 TextView 来使用自定义字体。问题是,我也会证明它是合理的。我试过像这样使用 html 代码:

testText.setText(Html.fromHtml("<html><head><div class=\"haodi_jianjie\";style=\"text-align:justify;text-justify:distribute-all-lines;color:white;background-color:black;\">asdsadsadsadsadsadsadsafsdvvbcvbcvb dfsdfsdgfgdfg  gdfghththgfhgfhgfhgfhfghgfhgfhgf sdfsdfsdfsdfsdfsdfsfwqewqewq</div></head></html>"));

但它不起作用。我不喜欢使用 web-view,因为透明背景对于 webview 来说是错误的,但我需要背景是透明的。

非常感谢。

最佳答案

Android TextView 不支持完全左右对齐。

您唯一的选择是切换到 WebView 或使用某种自定义 TextView。我不推荐自定义 TextView,因为这意味着您必须将其完全重写到核心。我建议您改用 WebView 或使用这样的库:https://github.com/bluejamesbond/TextJustify-Android


关于 TextView 中的 html:

Android TextView并不支持所有的html标签,只支持一些基本的标签。不支持 CSS。

有效的 html 标签:

<a href="...">
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align="...">
<em>
<font size="..." color="..." face="...">
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
<i>
<img src="...">
<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>

来源:http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

关于android - 如何在android textview中进行文本对齐并同时使用自定义字体样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26710607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com