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>