简体   繁体   中英

Dynamically Displaying Text with TextView (Android)

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:

    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.

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

There are two options for you.

One is

Spannable

and other is

fromHtml (String source)

So that you can get your desired output.

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.

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

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:

setContentView(R.layout.activity1);

// Create and adjust TextView text

...

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

Once a View is added to a ViewGroup of which LinearLayout is 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#invalidate and View#requestLayout where appropriate.

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.

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

Since you are creating your View s manually and not from xml you need to add your root ViewGroup to the Activity by calling Activity#setContentView .

Edit:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM