如何在LinearLayout中创建TextView填充剩余的空间并且是可截断的

问题描述 投票:0回答:2

我有一个包含一些行的ListView,特别是在2个TextView中组合类别和距离项的行:

  • 类别项可以包含一个或多个类别,因此这个TextView必须是可截断的
  • 只有在用户允许地理位置时才显示距离项,因此可以隐藏此TextView
  • 在这种情况下,类别项必须填充所有行的宽度

预期结果看起来像iOS上的地图应用程序的屏幕截图:Maps screenshot

我尝试使用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>   

有没有更好的方法来实现这一目标?

android textview width android-linearlayout truncate
2个回答
0
投票

试试这个:

<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类别扩展。


0
投票

我终于通过使用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>
© www.soinside.com 2019 - 2024. All rights reserved.