Skip to content Skip to sidebar Skip to footer

How Do I Alignbaseline To Bottom Line Of Multi-line Textview?

I have two TextViews side by side. Let's say the left TextView is 1 line and the right TextView is 2 lines. Is there any way to align the baselines of the left TextView with the la

Solution 1:

You could accomplish this with RelativeLayout if you implement a custom TextView. Specifically, you could override TextView.getBaseline like so:

package mypackage.name;

// TODO: imports, etc.publicclassBaselineLastLineTextViewextendsTextView {
    // TODO: constructors, etc.@OverridepublicintgetBaseline() {
        Layoutlayout= getLayout();
        if (layout == null) {
            returnsuper.getBaseline();
        }
        intbaselineOffset=super.getBaseline() - layout.getLineBaseline(0);
        return baselineOffset + layout.getLineBaseline(layout.getLineCount()-1);
    }
}

Then you would use your custom TextView inside a RelativeLayout as follows:

<mypackage.name.BaselineLastLineTextView
    android:id="@+id/text_view_1"
    <!-- TODO: other TextView properties -->/><TextView
    android:id="@+id/text_view_2"
    android:layout_alignBaseline="@id/text_view_1"
    <!-- TODO: other TextView properties -->/>

Solution 2:

I guess a RelativeLayout would do it.

<RelativeLayout..><TextViewandroid:id="@+id/tv2"android:layout_alignParentRight="true"android:maxLines="2"... /><TextViewandroid:id="@+id/tv1"android:layout_toLeftOf="@id/tv2"android:layout_alignBottom="@id/tv2"android:maxLines="1"... /></RelativeLayout>

Post a Comment for "How Do I Alignbaseline To Bottom Line Of Multi-line Textview?"