简体   繁体   中英

Add more than one String to a TextView

I have some strings in a xml, for example

<string name="String1">My String 1</string>
<string name="String2">My string 2</string>

and I want to show in the activity something like My String 1: My string 2

Is it possible to add to the same TextView more than a

<TextView android:text="@string/String1"/>
<TextView android:text=": "/>
<TextView android:text="@string/String2"/>

The problem of this is that if you insert them inside a TableLayout they are considered as cells and the ":" symbol is not written next to String1 (it's written in the middle of both Strings).

Is it possible to join the string in just one TextView in the xml code (without doing it programmatically in Java)? I mean is there any syntax to append strings something like

<TextView android:text="@string/String1+:+@string/String2"/>

Thanks

You cant do it directly in the xml, this is the best you can do:

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>


Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

I'm not 100%, but I dont think its possible to have more than one android:text per TextView.

If you are going to have three TextViews, you need to add something like to String2(or whatever view has the " : ":

android:layout_toRightOf="@id/string1"

See this related question I asked earlier.

It is not possible to do anything fancy with strings in XML. You need to do that in Java.

Look at the API for TableRow :

android:layout_span - Defines how many columns this child should span.

So I think you could use smth like:

<TableRow android:layout_span="3">
  <TextView android:id="@+id/your_entire_string" />
</TableRow>

Then in your Activity find the TextView by id and populate with ("My String 1" + ": " + "My String 2").

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