此布局似乎很简单,但是我找不到使用LinearLayout
或任何其他布局仅在xml中单独进行此操作的方法,而没有动态更改属性。这就是我想要的,如果文本很短,那么布局应该完全包裹起来,如图所示:
但是如果TextView
包含更长的文本,则应将其扩展,但必须为Button
留出空间,如下所示:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS82SHRGcS5wbmcifQ==” alt =“在此处输入图像描述”>
[尝试了很多布局之后,我能做的最好的就是将TableLayout
与shrinkColumns
结合使用
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="0" >
<TableRow>
<TextView
android:id="@+id/text"
android:ellipsize="end"
android:singleLine="true"
android:text="This is a very long text and it cannot be displayed wholly" />
<Button
android:id="@+id/button"
android:text="Button" />
</TableRow>
</TableLayout>
使textview的宽度wrapcontent
和给按钮toRightOf
文本视图然后文本视图将根据文本展开,并且按钮将始终位于文本视图的右侧
<TextView
android:id="@+id/tV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="400dp"
android:text="what ever you want" />
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@id/tV"
android:layout_alignParentRight="true"
android:text="Button" />
尝试此Wajeeh ........................
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:maxWidth="200dp"
android:text="kdfjdk" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
实现此目的的另一种方法是通过ConstraintLayout
,该问题在发布时不可用。请查看此答案https://stackoverflow.com/a/60163899/826606
<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"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="April Ludgate April April "
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/threadType"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap" />
<TextView
android:id="@+id/threadType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="SMS"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/title"
app:layout_constraintTop_toTopOf="@+id/title"
app:layout_constraintWidth_default="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>