How to delete one word from a android textview (multiple lines) without affecting the alignment/position of other words

huangapple go评论101阅读模式
英文:

How to delete one word from a android textview (multiple lines) without affecting the alignment/position of other words

问题

我有一个 Android 的 TextView,我正在向其设置一个包含 10 个单词的句子。

现在,我需要删除一个单词(比如第4个单词),而不影响其余单词的位置/对齐。

请参考下面的图片。还请注意,如果我将该特定单词替换为空格,仍然会导致其余单词的对齐稍微改变,因为每个字符的宽度都不同。

如何在 TextView 中实现这一点?任何帮助都将不胜感激。谢谢。

英文:

I have a android textview and I am setting 10 word sentence to it.

Now, I need to delete one word (say 4th word) without affecting the remaining word's positions/alignment.

Please refer below image. Also note, if am replacing that particular word with whitespaces, still alignment is slightly changing for remaining word's, as each character taking different width.

How can I achieve it using textview? Any help is much appreciated. Thanks.

How to delete one word from a android textview (multiple lines) without affecting the alignment/position of other words

答案1

得分: 2

你可以使用 SpannableString 来将背景颜色(在你的图片中看起来是白色)应用于应该消失的字母。

如果你想将这个效果应用于第四个单词("sample"),你可以这样写:

val startOfWord = 11
val endOfWord = 16
val spannable = SpannableString("This is my sample text goes ...")
spannable.setSpan(ForegroundColorSpan(Color.WHITE), 
     startOfWord, endOfWord + 1, 
     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

exampleTextView.text = spannable

另请参阅 Florina Muntenescu 的 Spantastic text styling with Spans

英文:

You can use a SpannableString to apply the color of the background (looks like white in your picture) to the letters which should vanish.

If you want to apply the effect to the fourth word ("sample") then you can write

val startOfWord = 11
val endOfWord = 16
val spannable = SpannableString(“This is my sample text goes ...”)
spannable.setSpan(ForegroundColorSpan(Color.WHITE), 
     startOfWord, endOfWord + 1, 
     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

exampleTextView.text = spannable

See also Spantastic text styling with Spans by Florina Muntenescu

答案2

得分: 0

这是一个算法:

  1. 首先: 使用 StaticLayout.getDesiredWidth() 计算要隐藏的单词的宽度。
  2. 其次: 使用相同的方法计算空格字符的宽度。
  3. 第三: 通过将第一个宽度除以第二个宽度,得到所需空格的数量。

让我们来编写代码:

注意:我使用了您提供的相同文本。

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is my sample text goes here\n with multiple words and lines and center aligned" />

Java

TextView textView = findViewById(R.id.textview);
String originalText = textView.getText().toString();
// 获取要替换的单词
String text = originalText.substring(11, 18);
// 步骤 1:获取单词宽度
float wordWidth = StaticLayout.getDesiredWidth(text, new TextPaint());
// 步骤 2:获取空格宽度
float spaceWidth = StaticLayout.getDesiredWidth(" ", new TextPaint());
// 步骤 3:获取所需空格数
float nSpaces = wordWidth / spaceWidth;

// 步骤 4:用空格数替换单词
StringBuilder newText = new StringBuilder(originalText.substring(0, 11));
for (int i = 0; i < nSpaces; i++) {
    newText.append(" ");
}

newText.append(originalText.substring(18));
textView.setText(newText);

结果:

How to delete one word from a android textview (multiple lines) without affecting the alignment/position of other words

英文:

Here is an algorithm

  1. First: use StaticLayout.getDesiredWidth() for calculating the width of the word that you want to be hidden
  2. Second: use the same method to calculate the width of the space character.
  3. Third: get the no. of needed spaces by dividing the first on the second.

Let's code:

Note: I used the same text as you put.

&lt;TextView
    android:id=&quot;@+id/textview&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:text=&quot;This is my sample text goes here\n with multiple words and lines and center aligned&quot; /&gt;

Java

TextView textView = findViewById(R.id.textview);
String originalText = textView.getText().toString();
// Get the word that you want to replace
String text = originalText.substring(11, 18);
// Step 1: Get the word width
float wordWidth = StaticLayout.getDesiredWidth(text, new TextPaint());
// Step 2: Get the space width
float spaceWidth = StaticLayout.getDesiredWidth(&quot; &quot;, new TextPaint());
// Step 3: Get the needed no. of spaces
float nSpaces = wordWidth / spaceWidth;

// Step 4: Replace the word with the no. of spaces
StringBuilder newText = new StringBuilder(originalText.substring(0, 11));
for (int i = 0; i &lt; nSpaces; i++) {
    newText.append(&quot; &quot;);
}

newText.append(originalText.substring(18));
textView.setText(newText);

Result

How to delete one word from a android textview (multiple lines) without affecting the alignment/position of other words

huangapple
  • 本文由 发表于 2020年7月24日 03:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63061843.html
匿名

发表评论

匿名网友

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

确定