简体   繁体   中英

Android: TextView word-wrap with shortest line first

For a graphical banner I am having two TextView s positioned on top of each other.

The top TextView is aligned to bottom and the bottom one is aligned to top so that the texts are always positioned around the same center. The TextViews look like this in the layout:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="fill_parent"
    android:paddingLeft="126dp"
    android:paddingRight="10dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLines="2"
        android:gravity="bottom|left"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:textSize="16sp" />
    <TextView
        android:id="@+id/artist"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:maxLines="2"
        android:gravity="top|left"
        android:textColor="#AAAAAA"
        android:textSize="16sp" />
</LinearLayout>

My problem is that when the top TextView wraps the text, the wrapping is with the last line as the smallest one, and not taking into account that I am aligning to the bottom. See below.

标语示例

I would have expected the gravity, to make the text wrapping happen in a way that has most of the text on the bottom line.

Any suggestions to how I could get the TextView to perform the word-wrapping in a last-line-first mannor?

Thanks for reading!

As far as I know, there are no last-line-first mannor as you need, however, there are some suggestion I would like to give:

Solution 1 : Let your text view (top one) be the single-line and with the ellipsize. This way, you will ensure your text view alway at the bottom of the container with a single line and ... at the end of the text. The drawback, the text may not displayed fully.

android:singleLine="true"
android:ellipsize="end"

Solution 2: You need to calculate the size of each character of your text, split your top text view into 2 text views, assign the text for those 2 text views. For example, in your case. After calculating the size of your text and compare with the size of the text view (the width in this case) , you will assign "The 20/20" to the top-top one, and the remain "Experience - 2 of 2 (Deluxe)" to top-bottom one. This can also apply to the long text with more than 2 lines. Just calculate and apply correctly, it will work. Here is the code for calculate the text size in pixel:

Paint p = new Paint();
        String your_text = "This is your text";
        float size = p.measureText(your_text);
        // assign one part to the top-top
        // assign another part to the top-bottom

Hope this helps.

// try this way
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

        <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                android:src="@drawable/ic_launcher"/>

        <LinearLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_marginLeft="5dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="First TextView"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="1dp"
                    android:text="Second TextView"/>

        </LinearLayout>
</LinearLayout>

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