【发布时间】:2019-12-12 16:49:56
【问题描述】:
我想要实现的目标很简单,但我无法输出它。
我想要一个简单的布局,带有固定宽度和高度的图标以及右侧的文本。轻松愉快。
我还想将文本相对于图标垂直居中。这也很容易。
当文本很短并且保持在一行中时,一切都是完美的。当文本较长时,问题就出现了。
使用此代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="fitXY"
android:src="@drawable/ic_icon"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. "
app:layout_constraintHeight_min="48dp"
app:layout_constraintLeft_toRightOf="@id/icon"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/icon" />
</androidx.constraintlayout.widget.ConstraintLayout>
根据需要显示一行文本,超过 48dp 时隐藏较长的文本
代码和我的知识有什么问题?
谢谢
【问题讨论】:
-
您是否尝试过使用包装内容的高度
-
@ManojMohanty 是的,没有理想的结果
-
I also want to center the text vertically with respect to the icon. Also this is easy.所以你的意思是你当前的实现正确地将文本相对于图标居中? -
我尝试过 textview 扩展,如果您将其高度设置为以 48dp 的最小高度包装内容,您是否有任何其他用于 ConstraintLayout 的父视图
标签: android android-layout android-constraintlayout