调整 Android 多行 TextView 的大小

发布于 2024-09-28 20:01:33 字数 1160 浏览 5 评论 0原文

我有一个活动,我想在其中显示一个标题,该标题占据应用程序的顶部四分之一并显示一组动态文本。在某些情况下,它会全部放在一行上,我希望字体大小变大,这样它就大且易于阅读。在某些情况下,它会出现在多行上(通常少于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久而酒知 2024-10-05 20:01:33

您可以计算字符串长度并设置文本大小以适合每个长度的最佳大小,

if(myString.length()==40){
    myTextview.setTextSize(11);
else if(myString.length()==25){
    myTextview.setTextSize(16);
...

希望这会有所帮助。

You could count the String lenght and set the text size to fit the best size for each lenght

something in this form

if(myString.length()==40){
    myTextview.setTextSize(11);
else if(myString.length()==25){
    myTextview.setTextSize(16);
...

I hope this helps.

暮年慕年 2024-10-05 20:01:33

你可以做到的。将新行字符附加到字符串值。然后将此字符串值设置为 TextView 的文本属性。

String temp = "ASDF \n"+"EFG \n"+"HIJ";
TextView tv=//find it from the context
tv.setText(temp);

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.

String temp = "ASDF \n"+"EFG \n"+"HIJ";
TextView tv=//find it from the context
tv.setText(temp);
掩饰不了的爱 2024-10-05 20:01:33

还有一种解决方案,您可以使用 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

三生殊途 2024-10-05 20:01:33

让我回顾一下您的问题,以确保我正确理解您的意思。

  1. 你的输入总是 1 到 4 行文本,
  2. 如果你有不止一行,你总是只想在行尾中断,
  3. 你希望每一行尽可能大,以便它适合文本视图,即 1/ 4.屏幕的高度?

我建议您制作一个 自定义视图

  • 使用
  • >Paint.getTextBounds 要在代码中动态确定用于绘制文本的 您可以使用帮助器类 StaticLayout 用于控制对齐等。
  • 要让自定义视图占据屏幕的 1/4,请将其放入 LinearLayout 中并使用layout_weight 为其分配高度,或者在 View.onMeasure

Let me recap your question to make make sure i understood you correctly.

  1. your input is always 1 to 4 lines of text
  2. you always and only want to break at the end of a line if you have more than one line
  3. you want to be each line as large as possible so that it fits the textview which is 1/4 of the height screen?

I propose that you make a custom view:

  • use Paint.getTextBounds to determine the best text size dynamically in code
  • for drawing the text you can use the helper class StaticLayout for controlling alignment and such.
  • to have you custom view take up 1/4 of the screen either put it inside a LinearLayout and use the layout_weight to assign it its height or set it programatically in View.onMeasure
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文