我有一个包含一些行的ListView
,特别是在2个TextView
中组合类别和距离项的行:
TextView
必须是可截断的TextView
我尝试使用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>