指定 外部Textview 对齐指定TextView 最后一行

UI如图 ,需求是这个 “详情” Textview 能和多行简介的最后一行对齐,并且能够获得焦点

 

获得最后一行的位置 

关键是获得简介内容的最后一行的位置:

可参见:

TextView 实现最后一行缩进指定距离_textview 行末缩进-CSDN博客

通过自定义绘制,可以在最后一行绘制的时候,获得实际的 lineY

 

注意,这里的 mLineY坐标基线 baseLine

Y 坐标:是文本的基线位置,而不是文本的顶部或底部。文本的基线是大多数字体中字符下部的参考线。

当获得最后一行的基线之后,附加的 textview的位置非常好确认:

// 计算 more 按钮的位置:
// 简介的最后一行的 Y 基线坐标 + 简介的 top 坐标 - 简介的行高的一半 - 5dp(额外的行高) - 2dp (详情文字的 padding)
mMore.y = mIntroduction.mLastLineY + mIntroduction.top - (mIntroduction.lineHeight / 2) - 5.dp - 2.dp
                    

 

 

XML 的内容如下: 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    <!--简介信息位置-->
    <com.xxx.library.uibase.widget.CusTextview
        android:id="@+id/base_info2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lineSpacingExtra="5dp"
        android:maxLines="3"
        android:textSize="17sp" />

    <!--详情/更多按钮-->
    <TextView
        android:id="@+id/more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/base_info2"
        android:layout_alignTop="@id/base_info2"
        android:background="@drawable/sel_more_bg"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingLeft="10dp"
        android:paddingTop="2dp"
        android:paddingRight="10dp"
        android:paddingBottom="2dp"
        android:text="详情>"
        android:textSize="17sp"
        android:visibility="invisible" />

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值