java 使用 TextView 动态显示文本 (Android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4731779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:46:07  来源:igfitidea点击:

Dynamically Displaying Text with TextView (Android)

javaandroidandroid-layouttextview

提问by Rob S.

Basically what I want to do in my Android app is use TextView to display two different pieces of text at once. So in code, I want to be able to do something like this:

基本上我想在我的 Android 应用程序中做的是使用 TextView 一次显示两个不同的文本。所以在代码中,我希望能够做这样的事情:

    LinearLayout ll = new LinearLayout(this);
    TextView text = new TextView(this);
    text.setTextColor(0xFF000000);
    text.setGravity(Gravity.CENTER_HORIZONTAL);
    text.setTextSize(20f);
    text.setText("Text1");
    text.setTextSize(14f);
    text.setColor(0xFF0000FF);
    text.setText("\nText2");
    ll.addView(text);

To clarify, I am trying to display a black "Text1" and a blue "Text2" at once using only a single TextView. Obviously this doesn't work out using the code above. I've considered using a second TextView but that seems like a waste of effort and memory to me. I'm sure the brilliant minds here can come up with the best solution to this.

为了澄清起见,我试图仅使用一个 TextView 一次显示一个黑色的“Text1”和一个蓝色的“Text2”。显然,使用上面的代码是行不通的。我已经考虑使用第二个 TextView,但这对我来说似乎是在浪费精力和内存。我相信这里的聪明人可以想出最好的解决方案。

Thank you very much in advance for your time and your assistance.

非常感谢您的时间和帮助。

回答by Tanmay Mandal

There are two options for you.

有两种选择。

One is

一个是

Spannable

可跨越

and other is

另一个是

fromHtml (String source)

fromHtml(字符串源)

So that you can get your desired output.

这样你就可以得到你想要的输出。

回答by TheCottonSilk

I think with the current version of the code, you can see only the latest text (Text2). If you want to have multiple look and feel for two texts, I would suggest use 2 separate TextViews. It would add more flexibility.

我认为使用当前版本的代码,您只能看到最新的文本(Text2)。如果您想对两个文本有多种外观和感觉,我建议使用 2 个单独的 TextViews。它会增加更多的灵活性。

If you are not going to change this UI code later, then you can consider Html.toHtml() in setText() call.

如果以后不打算更改此 UI 代码,则可以考虑在 setText() 调用中使用 Html.toHtml()。

回答by Piva

It seems the problem is with:

问题似乎在于:

LinearLayout.addView(text);

You are trying to add a view to a LinearLayout, but the layout doesn't exist (in the current activity). You need to add the TextView to a Layout defined in the .xml you are using. Suppose you have a LinearLayout with id "linearlayout01" in the xml file "activity1.xml", you would do something like:

您正在尝试向 LinearLayout 添加视图,但该布局不存在(在当前活动中)。您需要将 TextView 添加到您正在使用的 .xml 中定义的布局。假设您在 xml 文件“activity1.xml”中有一个 id 为“linearlayout01”的 LinearLayout,您可以执行以下操作:

setContentView(R.layout.activity1);

// Create and adjust TextView text

...

LinearLayout layout = (LinearLayout) findViewById(R.id.linearlayout01);
layout.addView(text);

回答by Rich Schuler

Once a Viewis added to a ViewGroupof which LinearLayoutis a descendant you do not need to readd it to update its display. If you preform any changes on a view that requires it to change its display it will handle all the proper notifications about required redraws or relayouts by calling View#invalidateand View#requestLayoutwhere appropriate.

一旦将 aView添加到ViewGroup其中LinearLayout一个是后代的,您就不需要读取它来更新其显示。如果您对瓶坯需要通过调用来改变其显示它会处理所有有关所需重绘或relayouts适当的通知视图的任何变化View#invalidate,并View#requestLayout在适当情况下。

In addition, because all UI changes are handled on the same thread you do not need to worry about calling multiple methods that will update the UI. This is because of two reasons, first, the execution of the redraws will not occur until your code is finished, second, android has optimizations built in that combines multiple invalidate calls into one.

此外,由于所有 UI 更改都在同一线程上处理,因此您无需担心调用将更新 UI 的多个方法。这是因为两个原因,首先,在您的代码完成之前不会执行重绘,其次,android 内置了优化,将多个无效调用合并为一个。

So, the only thing you need to worry about is getting a proper reference to your TextViewinstance and then you can call all the methods on it that you need to make it display what you wish.

因此,您唯一需要担心的是获得对您的TextView实例的正确引用,然后您可以调用其上的所有方法以使其显示您想要的内容。

Since you are creating your Views manually and not from xml you need to add your root ViewGroupto the Activityby calling Activity#setContentView.

因为您创建View的XML手动,而不是你需要进行根添加ViewGroupActivity通过调用Activity#setContentView

Edit:

编辑:

Then you're going to need to learn about SpannableStringand SpannableStringBuilder. There is some very brief documentation here: Selecting, Highlighting, or Styling Portions of Text

那么你将需要了解SpannableStringSpannableStringBuilder。这里有一些非常简短的文档:选择、突出显示或样式部分文本

回答by pankajagarwal

when do you plan to update the textview? If it is on click of a button then get a reference to the textviewand in the onClickListener() update the text, color, etc whatever you want to do.

你打算textview什么时候更新?如果是单击按钮,则获取textview对 onClickListener()的引用,并在 onClickListener() 中更新文本、颜色等,无论您想做什么。

After seeing your other comments, I think SpannableStringis what you are looking for

看到你的其他评论后,我认为SpannableString就是你要找的