效果展示

先来看看上面的效果
左侧的文字宽度是自适应的,但是右侧又有一个TextView
左侧的文字被限制不能把右侧的挤出屏幕外面
所以如果左侧文字超过指定宽度后多余部分就用省略号表示
实际开发中这种情况在一些列表的item中用的比较多
但实际实现的时候会发现
左侧的总是会把右侧的给挤出去
后来用到了ConstraintLayout布局的链条样式来解决这个问题
ConstraintLayout解决办法
因为代码不是很多,我就直接贴出来了
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左侧文字较短时:"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/left1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="1"
android:text="左侧文字"
android:padding="4dp"
android:textColor="#f00"
app:layout_constraintEnd_toStartOf=

博客主要围绕Android开发中文字布局问题展开。在列表item里,左侧自适应宽度文字常把右侧挤出屏幕。介绍了两种解决办法,一是用ConstraintLayout布局的链条样式,需写好左右TextView的start和end约束;二是用LinearLayout,其外层宽度固定或match即可。
最低0.47元/天 解锁文章
1811

被折叠的 条评论
为什么被折叠?



