调整 Android 多行 TextView 的大小
我有一个活动,我想在其中显示一个标题,该标题占据应用程序的顶部四分之一并显示一组动态文本。在某些情况下,它会全部放在一行上,我希望字体大小变大,这样它就大且易于阅读。在某些情况下,它会出现在多行上(通常少于 4 行)并且需要更小。
复杂的是,如果我在 setText 中显式设置换行符,有时它会在我没有预料到的地方插入一个换行符,即:
"My first line of text\nMy Second line of text" Might get turned into (it's right adjusted in this version): "My first line of text My second line of text"
我不知道如何阻止 TextView 尝试插入换行符或让它只是负责格式化整个内容。如果我将一些文本设置为一个非常长的单个单词,它会在某个选定点的中间插入一个换行符。有时我只是随机获取最后几行文本。
我是否应该明确地将字体大小设置为非常小以使其正确布局,然后手动调整大小?我应该为此使用“最佳实践”吗?如果我将字体大小设置为相当大的字体,它会起作用,但最好弄清楚执行此操作的“正确”方法是什么。我不想手动添加 4 个单行 TextView 并自己格式化它们。
我使用的布局 xml 是:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dip"
android:layout_marginRight="8dip"
android:singleLine="false"
android:ellipsize="marque"
android:gravity="right|bottom"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30sp"
/>
I have an Activity where I want to display a heading that takes up the top quarter of the app and displays a dynamic set of text. In some cases it'll all fit on one line and I'd like the font size to get sized up so it's large and easy to read. In some cases it'll be on multiple lines (usually less than 4) and need to be smaller.
The complication is that if I explicitly set the newlines in setText sometimes it will insert a newline where I didn't expect one, i.e.:
"My first line of text\nMy Second line of text" Might get turned into (it's right adjusted in this version): "My first line of text My second line of text"
I'm not sure how to go about either keeping the TextView from trying to insert newlines or letting it just take care of formatting the whole thing. If I set some text to be one really long single word it'll insert a newline in the middle at some chosen point. And other times I just sort of randomly get the last few lines of text.
Should I just explicitly set the font size to be really small to get it laid out correctly and then manually adjust the size? Is there a 'best practice' that I should use for this? If I set the font size to be a reasonably large font it kind of works but it would be nice to figure out what the 'right' way to do this would be. And I'd rather not have to manually add in 4 single line TextViews and format them myself.
The layout xml I'm using is:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dip"
android:layout_marginRight="8dip"
android:singleLine="false"
android:ellipsize="marque"
android:gravity="right|bottom"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="30sp"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以计算字符串长度并设置文本大小以适合每个长度的最佳大小,
我
希望这会有所帮助。
You could count the String lenght and set the text size to fit the best size for each lenght
something in this form
I hope this helps.
你可以做到的。将新行字符附加到字符串值。然后将此字符串值设置为 TextView 的文本属性。
You can do it. Append a new line charecter to the string value. Then set this string value as the text attribute of the TextView.
还有一种解决方案,您可以使用 WebView 执行您想要的操作,请检查此 Android TextView Justify Text 详细回答。使用 webview 你不必关心行对齐,你可以通过 html css 属性来管理它
There is one more solution that you can use WebView to do what you want check this Android TextView Justify Text answer for details. using webview you dont have to care about line alignments you can manage that by html css property
让我回顾一下您的问题,以确保我正确理解您的意思。
我建议您制作一个 自定义视图:
Let me recap your question to make make sure i understood you correctly.
I propose that you make a custom view: