html靠中上对齐,Html.fromHtml()中的对齐方式

7 个答案:

答案 0 :(得分:16)

TextView中的HTML文本不支持对齐。

答案 1 :(得分:8)

要在textview中设置对齐,请先将textview width设置为match_parent,然后在HTML标记中使用text-align选项。像这样的东西:

embedded_swf['player_load']({

'vast': vast, // parsed VAST

'callback': 'window.player.events.backchannel' // some available method to send success/error messages to

});

[...]

embedded_swf['player_exec']({

'command': {

'command': 'play',

'target': 'ad.id.1' // some identifier flash-player can work with

},

'callback': 'window.player.events.backchannel'

});

更新:

这个解决方案适用于android 7及以上版本:|

答案 2 :(得分:2)

虽然可以使用webview而不是TextView来使用对齐,并从放置在资源中的文件加载html:

public class ViewWeb extends Activity {

WebView webview;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.webview);

webview = (WebView) findViewById(R.id.webView1);

webview.loadUrl("file:///android_asset/index.html");

}

}

答案 3 :(得分:2)

对于API 24及更低版本,将gravity属性添加到TextView布局元素

android:gravity="center_horizontal"

对于API 24及更高版本,可以在HTML中使用style。

String centeredHtml = "

" + myFormattedHtml + "
";

textView.setText(Html.fromHtml(centeredHtml));

但至少API 26也支持gravity。

答案 4 :(得分:2)

您无法在HTML文本中设置对齐方式,但可以使用SpannableStringBuilder代替。它很冗长,但它完成了工作。

E.g。

private Spanned getFormattedLabelText(String text, String subText) {

String fullText = String.format("%s\n%s", text, subText);

int fullTextLength = fullText.length();

int titleEnd = text.length();

SpannableStringBuilder s = new SpannableStringBuilder(fullText);

// Center align the text

AlignmentSpan alignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);

s.setSpan(alignmentSpan, 0, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Make the title bold

s.setSpan(new StyleSpan(Typeface.BOLD), 0, titleEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold

// Make the subtext small

int smallTextSize = DisplayUtil.getPixels(TypedValue.COMPLEX_UNIT_SP, 10);

s.setSpan(new AbsoluteSizeSpan(smallTextSize), titleEnd + 1, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //bold

return s;

}

然后照常设置TextView文本:

myTextView.setText(getFormattedLabelText("Title", "Subtitle"));

答案 5 :(得分:0)

If someone wants to align text in center with color red then one may use code below.

String centerNRed = "

Hello World Its me.....
";

txt1.setText(Html.fromHtml(centerNRed));

答案 6 :(得分:0)

我相信更容易使用TextView中的Gravity属性作为文本的中心...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值