如何将BaseBaseline与多行TextView的底线对齐? [英] How do I alignBaseline to bottom line of multi-line TextView?

查看:100
本文介绍了如何将BaseBaseline与多行TextView的底线对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并排有两个TextViews.假设左边的TextView为1行,右边的TextView为2行.有什么方法可以将左侧TextView的基线与右侧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 last line in the right TextView?

我愿意使用可以完成此操作的任何布局(RelativeLayout,LinearLayout等).

I'm open to using whatever layout (RelativeLayout, LinearLayout, etc.) that can accomplish this.

(Android中 android:layout_alignBaseline 的默认行为是它对齐顶行的基线.)

(The default behavior of android:layout_alignBaseline in Android is that it aligns the baselines of the top lines.)

推荐答案

如果实现自定义TextView,则可以使用RelativeLayout完成.具体来说,您可以覆盖 TextView.getBaseline 像这样:

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.

public class BaselineLastLineTextView extends TextView {
    // TODO: constructors, etc.

    @Override
    public int getBaseline() {
        Layout layout = getLayout();
        if (layout == null) {
            return super.getBaseline();
        }
        int baselineOffset = super.getBaseline() - layout.getLineBaseline(0);
        return baselineOffset + layout.getLineBaseline(layout.getLineCount()-1);
    }
}

然后,您将在RelativeLayout中使用自定义TextView,如下所示:

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 -->
/>

这篇关于如何将BaseBaseline与多行TextView的底线对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆