如何使 LinearLayout 中的 TextView 填充剩余的 space 并可截断
How to make a TextView in a LinearLayout fill the remaining space and being truncable
我有一个 ListView
包含一些行,尤其是包含 类别 和 距离 项的行 [=2TextView
:
- 类别 项目可以包含一个或多个类别,因此此
TextView
必须 truncable
- 距离项目仅在用户允许地理定位时显示,所以这个
TextView
可以隐藏
- 在这种情况下,类别 项目必须填充所有 行的宽度
预期结果类似于 iOS 上地图应用程序的屏幕截图:
我已经尝试使用 LinearLayout 来执行此操作,但这并没有按预期工作:
<LinearLayout android:id="@+id/AffiliateCellDetail"
android:layout_below="@id/AffiliateCellTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<TextView android:id="@+id/AffiliateCellCategories"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ellipsize="end"
android:singleLine="true"/>
<TextView android:id="@+id/AffiliateCellDistance"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
有更好的方法吗?
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AffiliateCellDetail"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="@+id/AffiliateCellCategories"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/AffiliateCellDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
android:layout_weight="1"
将使类别 TextView 在有地方填充时展开。
我终于通过使用ConstraintLayout
实现了这一点:
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/AffiliateCellDetail"
android:layout_below="@id/AffiliateCellTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<TextView android:id="@+id/AffiliateCellCategories"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:ellipsize="end"
android:singleLine="true"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/AffiliateCellDistance"/>
<TextView android:id="@+id/AffiliateCellDistance"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintBaseline_toBaselineOf="@+id/AffiliateCellCategories"
app:layout_constraintLeft_toRightOf="@+id/AffiliateCellCategories"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
我有一个 ListView
包含一些行,尤其是包含 类别 和 距离 项的行 [=2TextView
:
- 类别 项目可以包含一个或多个类别,因此此
TextView
必须 truncable - 距离项目仅在用户允许地理定位时显示,所以这个
TextView
可以隐藏 - 在这种情况下,类别 项目必须填充所有 行的宽度
预期结果类似于 iOS 上地图应用程序的屏幕截图:
我已经尝试使用 LinearLayout 来执行此操作,但这并没有按预期工作:
<LinearLayout android:id="@+id/AffiliateCellDetail"
android:layout_below="@id/AffiliateCellTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<TextView android:id="@+id/AffiliateCellCategories"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ellipsize="end"
android:singleLine="true"/>
<TextView android:id="@+id/AffiliateCellDistance"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
有更好的方法吗?
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AffiliateCellDetail"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="@+id/AffiliateCellCategories"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="@+id/AffiliateCellDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
android:layout_weight="1"
将使类别 TextView 在有地方填充时展开。
我终于通过使用ConstraintLayout
实现了这一点:
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/AffiliateCellDetail"
android:layout_below="@id/AffiliateCellTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<TextView android:id="@+id/AffiliateCellCategories"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:ellipsize="end"
android:singleLine="true"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/AffiliateCellDistance"/>
<TextView android:id="@+id/AffiliateCellDistance"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintBaseline_toBaselineOf="@+id/AffiliateCellCategories"
app:layout_constraintLeft_toRightOf="@+id/AffiliateCellCategories"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>