在Android Studio(Java)中有没有一种方法可以给文本添加上划线?

huangapple 未分类评论56阅读模式
标题翻译

Is there a way to overline text in android studio (java)

问题

我正在尝试在我的 Java 移动应用程序中显示平方根。我尝试使用以下 HTML 代码。它会显示平方根符号,但变量(√100)上面没有横线。有办法在变量 (x) 上面添加横线吗?感谢您的帮助 在Android Studio(Java)中有没有一种方法可以给文本添加上划线?
TextView.settext(Html.fromHtml("&#x221A " + x));

英文翻译

I am trying to display square roots in my java mobile aplication. I tried with HTML by typing the code below. It displays the square root symbol but there is no line over the variable (√100). Is there a way to overline the variable (x). Thanks for your help 在Android Studio(Java)中有没有一种方法可以给文本添加上划线?
TextView.settext(Html.fromHtml("&#x221A "+x));

答案1

得分: 0

你可以尝试像这样使用StrikethroughSpan:

  1. textView.setText(x.toString(), TextView.BufferType.SPANNABLE);
  2. Spannable spannable = (Spannable) textView;
  3. spannable.setSpan(new StrikethroughSpan(), 0, 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
英文翻译

You can try StrikethroughSpan like this:

  1. textView.setText(x.toString(), TextView.BufferType.SPANNABLE);
  2. Spannable spannable = (Spannable) textView;
  3. spannable.setSpan(new StrikethroughSpan(), 0, 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

答案2

得分: 0

在Android中显示数学公式可能最简单的方法是使用Web视图,然后使用MathML。例如:

  1. <math>
  2. <msqrt>
  3. <mn>100</mn>
  4. </msqrt>
  5. </math>

这将显示一个格式良好的数学表达式,其中包含100的平方根。

您可以在这里尝试:

https://www.w3schools.com/code/tryit.asp?filename=GFAL3VTCXMCU

然后点击运行。

然而,对于您的目的来说,WebView和MathML可能过于复杂,因此阅读或浏览https://developer.mozilla.org/en-US/docs/Web/MathML/Authoring以及使用WebView的缺点也可能是一个好主意。

英文翻译

Showing math formulas in Android might most easily be done by using a web view and then using MathML. For instance:

  1. <math>
  2. <msqrt>
  3. <mn>100</mn>
  4. </msqrt>
  5. </math>

should show a well-formatted mathematical expression consisting of a square root of 100.

You can try it out here:

https://www.w3schools.com/code/tryit.asp?filename=GFAL3VTCXMCU

and then click run.

WebView and MathML may be overkill for your purposes, however, and it may also be a good idea to read or skim through https://developer.mozilla.org/en-US/docs/Web/MathML/Authoring as well as the drawbacks of using a WebView.

答案3

得分: 0

在代码中添加:

  1. TextView.settext(Html.fromHtml("&#x221A " + x));

将以下内容添加到你的代码中以实现上划线的文本。你可以使用SPAN、P或DIV,而不是SPAN:

  1. <SPAN STYLE="text-decoration:overline">文本在此处具有上划线。</SPAN>
英文翻译

Inside:

  1. TextView.settext(Html.fromHtml(&quot;&amp;#x221A &quot;+x));

Add

  1. &lt;SPAN STYLE=&quot;text-decoration:overline&quot;&gt;here the text has overline.&lt;/SPAN&gt;

to your code for over-lined text. Instead of SPAN, you can use P or DIV.

答案4

得分: 0

我能够通过这个答案的一个小变通方法来完成。

以下是如何为以后遇到这个问题的任何人执行的步骤:

步骤 1: 创建一个可绘制文件,并根据需要为其命名,这里我将其命名为 overline.xml

  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item
  3. android:bottom="-2dp"
  4. android:left="-2dp"
  5. android:right="-2dp"
  6. android:top="1dp">
  7. <shape android:shape="rectangle">
  8. <stroke
  9. android:width="1dp"
  10. android:color="#030310" />
  11. </shape>
  12. </item>
  13. </layer-list>

步骤 2: 将 overline.xml 添加为 TextView 的背景,使用 android:background="@drawable/overline"

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:background="@drawable/overline"
  5. android:textColor="#030310"
  6. android:text="Hello"/>

结果:

在Android Studio(Java)中有没有一种方法可以给文本添加上划线?

英文翻译

I was able to do it with a little workaround from this answer.

Here is how it can be done for anyone coming later to this question

Step 1: Create a drawable file and name it whatever you wish to, in this case I am naming it as overline.xml

  1. &lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  2. &lt;item
  3. android:bottom=&quot;-2dp&quot;
  4. android:left=&quot;-2dp&quot;
  5. android:right=&quot;-2dp&quot;
  6. android:top=&quot;1dp&quot;&gt;
  7. &lt;shape android:shape=&quot;rectangle&quot;&gt;
  8. &lt;stroke
  9. android:width=&quot;1dp&quot;
  10. android:color=&quot;#030310&quot; /&gt;
  11. &lt;/shape&gt;
  12. &lt;/item&gt;
  13. &lt;/layer-list&gt;

Step 2: Add the overline.xml as a background onto your TextView with android:background=&quot;@drawable/overline

  1. &lt;TextView
  2. android:layout_width=&quot;wrap_content&quot;
  3. android:layout_height=&quot;wrap_content&quot;
  4. android:background=&quot;@drawable/overline&quot;
  5. android:textColor=&quot;#030310&quot;
  6. android:text=&quot;Hello&quot;/&gt;

Result:

在Android Studio(Java)中有没有一种方法可以给文本添加上划线?

答案5

得分: 0

我遇到了相同的问题。恐怕对我没有起作用:

  1. <SPAN STYLE="text-decoration:overline">这里的文字有上划线。</SPAN>

这个建议可能来自这里:https://html-shark.com/HTML/Overline.htm

我还找到了这个提供有关 HTML 支持的标签的更多信息的链接:
https://stackoverflow.com/questions/25011611/html-string-with-style-tag-and-style-in-android-textview/25011685

根据上面链接的讨论(不幸的是),上划线的 span 样式似乎需要 CSS,而在 Android 中不受支持。

在 XML 背景行上的解决方法似乎是一个外观不错的解决方案,但对我也没有起作用 - 我需要在字符串中随机地添加上划线的单词。

虽然看起来不完美,但我现在是逐个字符使用上划线。例如,要在单词 'Test' 的每个字符上加上划线,使用:

  1. editText.setText(Html.fromHtml("T&#773;e&#773;s&#773;t&#773;"));

维基百科上还有其他关于上划线的选项:
https://en.wikipedia.org/wiki/Overline

对其他人也可能有用...

英文翻译

I had the same issue. I am afraid, this has not worked for me:

&lt;SPAN STYLE=&quot;text-decoration:overline&quot;&gt;here the text has overline.&lt;/SPAN&gt;

The suggestion may come from here?: https://html-shark.com/HTML/Overline.htm

I also came across this one which provides some more information on html supported tags:
https://stackoverflow.com/questions/25011611/html-string-with-style-tag-and-style-in-android-textview/25011685

The overline span style seems to require css which is according to above linked discussion (unfortunately) not supported in Android.

The work around with the xml background line looks like a good looking solution but also did not work for me - I need overlined words randomly in strings.

Not looking perfect, but I have used overline each individual character instead now. However, this can leave gaps between different overlines. For example, to overline every character in the word 'Test', use:

editText.setText(Html.fromHtml(&quot;T&amp;#773;e&amp;#773;s&amp;#773;t&amp;#773;&quot;));

other overline options on wikipedia:
https://en.wikipedia.org/wiki/Overline

May work for others as well...

huangapple
  • 本文由 发表于 2020年5月30日 16:36:47
  • 转载请务必保留本文链接:https://java.coder-hub.com/62099917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定