开发者

Add more than one String to a TextView

开发者 https://www.devze.com 2023-01-25 08:08 出处:网络
I have some strings in a xml, for example <string name=\"String1\">My String 1</string> <string name=\"String2\">My string 2</string>

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 开发者_如何学Goin 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").

0

精彩评论

暂无评论...
验证码 换一张
取 消