如何实现
怎么实现这种需求呢?最笨的办法就是动态计算,很麻烦而且导致代码很不优雅。就是动态计算出TextView的宽度。
有没有从布局上就能实现上图的需求呢?
下面文章中方案还没试,以后碰到再试
2 TextViews, left with ellipsis, right with nowrap, single line
2022年9月2日更新
碰到一个和上面一模一样的需求,按照上面文章的实现方案可以实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_user_action"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_comment_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/c_4a90e2"
android:textSize="13sp"
android:ellipsize="end"
android:maxLines="1"
android:includeFontPadding="false"
android:text="迪丽热"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tv_comment_user_action"/>
<TextView
android:id="@+id/tv_comment_user_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/c_999999"
android:textSize="13sp"
android:layout_marginLeft="2dp"
android:includeFontPadding="false"
android:text="点赞了你的评论"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tv_comment_user_name"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
一层完美实现
方案二需要两层实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_comment_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/c_4a90e2"
android:textSize="13sp"
android:layout_weight="1.0"
android:ellipsize="end"
android:maxLines="1"
android:includeFontPadding="false"
android:text="迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴迪丽热巴" />
<TextView
android:id="@+id/tv_comment_user_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/c_999999"
android:textSize="13sp"
android:layout_marginLeft="2dp"
android:includeFontPadding="false"
android:text="点赞了你的评论" />
</LinearLayout>
</LinearLayout>
2021年3月3日更新
在开发中又遇到了类似的需求,看下图
左右两边宽度固定后,需要中的内容自适应。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ImageView
android:id="@+id/guide_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_11"
android:src="@drawable/icon_more_2"
app:layout_constraintRight_toRightOf="@+id/guide_other_text_view"
app:layout_constraintTop_toBottomOf="@+id/guide_other_text_view">
</ImageView>
<RelativeLayout
android:id="@+id/guide_portrait_container"
android:layout_width="102dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginTop="@dimen/dp_15"
android:layout_marginBottom="@dimen/dp_30"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
</RelativeLayout>
<TextView
android:id="@+id/guide_other_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_15"
android:textColor="@color/bg_1a1a1a"
android:textSize="@dimen/sp_13"
app:layout_constraintBottom_toBottomOf="@+id/guide_portrait_container"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guide_portrait_container">
</TextView>
<TextView
android:id="@+id/guide_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_7"
android:ellipsize="end"
android:includeFontPadding="false"
android:singleLine="true"
android:textColor="@color/bg_1a1a1a"
android:textSize="@dimen/sp_13"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="@+id/guide_portrait_container"
app:layout_constraintLeft_toRightOf="@+id/guide_portrait_container"
app:layout_constraintRight_toLeftOf="@+id/guide_other_text_view"
app:layout_constraintTop_toTopOf="@+id/guide_portrait_container">
</TextView>
<TextView
android:id="@+id/guide_goto_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_3"
android:text="去关注流看看"
android:textSize="@dimen/sp_12"
android:textColor="@color/c_4a90e2"
app:layout_constraintBottom_toBottomOf="@+id/guide_arrow"
app:layout_constraintRight_toLeftOf="@+id/guide_arrow"
app:layout_constraintTop_toTopOf="@+id/guide_arrow">
</TextView>
<View
android:layout_width="0dp"
android:layout_height="1px"
android:background="#FFE6E6E6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
看guide_title_text_view设置了左右相对位置后,如果layout_width设置的是wrap_content,需要设置layout_constrainedWidth为true,否则guide_title_text_view过长的部分会盖到左右两边的view上;或者把layout_width设置成0dp。
0dp就是match parent,内容在左边;wrap content 内容是居中的。
参考
2 TextViews, left with ellipsis, right with nowrap, single line