简体   繁体   中英

Android - How to append the textview to the end of another textview?

I am creating a Gallery application. Each image will show the title along with the name of the person who uploaded the image. Example of what I want is below:

在此处输入图像描述

Here the Night is dark, but this darkness has the silence is the title for the image, and the shaggy boy is the user who uploaded the image for example.

So, now I want Night is dark, but this darkness has the silence to be in the separate textview and the shaggy boy at the end of the Night is dark, but this darkness has the silence 's textview even if Night is dark, but this darkness has the silence is a single line or multi-line.

I have used the Flexbox Layout but I am failed to achieve what I want.

How to append the textview to the end of another textview ? Thanks!!!

You can use a single TextView and set its text String with a Spannable and match each portion of text with indices here I am separating both portions by a hyphen; but you can change that as you would like.

TextView textView = findViewById(...);

String tag = "Night is dark, but this darkness has the silence - shaggy boy";
SpannableString spannableString = new SpannableString(tag);

spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, tag.indexOf("-"), 0);
spannableString.setSpan(new ForegroundColorSpan(Color.GRAY), tag.indexOf("-"), tag.length(), 0);
textView.setText(spannableString);

Maybe you could just get the text of the author's TextView and add it to the String of the first TextView?

You can use a single TextView with String palceholders of the quote and the author, and add the colors using HTML tags.

strings.xml

<resources>

    <string name="tag">
        <![CDATA[
            <font color="#00000">%1$s</font> -
            <font color="#D3D3D3">%2$s</font>
        ]]>
    </string>


</resources>

Behavior:

String message = "Night is dark, but this darkness has the silence";
String name = "shaggy boy";

String formattedTag = String.format(getString(R.string.tag), message, name);

textview.setText(Html.fromHtml(formattedTag));

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