【发布时间】:2019-12-21 07:42:29
【问题描述】:
有 4 个视图 A (TextView)、B(icon)、C(TextView)、D (TextView) 水平对齐彼此相邻。
B 和 D 具有固定宽度。文字会填充到onBindView()中。
可以根据屏幕方向计算其父级的宽度。
它需要尽可能多地显示 A,C 将消耗其余部分或直到其内容宽度(如果空间不足,则显示省略号)。
并且所有 A、B、C、D 都应该左对齐前一个的右边缘。
有少数情况,但这四个涵盖了大部分情况。
1. Parent width is enough for all
|-|AAAA|B|CCCCCCCC|DDD|--------------|-|
2. C is too large and have to show ellipses
|-|AAAAA|B|CCCCCCCCCCCCC...|DDD|-|
3. A is large and have to show ellipse for C to be able to show
|-|AAAAAAA..........|B|CCCCCCCC|DDD|-|
4. the parent width is not enough for showing A and C completely, so A and C both show ellipses
|-|AAAAAAAAAAAAAA...|B|CCCCC...|DDD|-|
所以规则是尽可能地显示 A,C 要么全部显示要么用省略号显示,以防显示 C 的 minWidth 仍然不能完整显示 A,然后用省略号显示 A。
试过LinearLayout、RelativeLayout和ConstraintLayout都没有得到想要的结果。
这对于 A 和 C 有两个 maxWidth,问题是如果 A 或 C 太小,另一个可能在不使用所有可用空间的情况下仍然显示省略号。
<LinearLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/a_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxWidth="130dp"
android:maxLines="1"
android:textSize="16sp"
android:textStyle="bold"
tools:text="ee" />
<ImageView
android:id="@+id/b_checked"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_marginStart="3dp"
android:layout_marginEnd="8dp"
android:baselineAlignBottom="true"
app:srcCompat="@drawable/checked" />
<TextView
android:id="@+id/c_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:ellipsize="end"
android:maxWidth="150dp"
android:maxLines="1"
tools:text="aasdasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfa" />
<TextView
android:id="@+id/d_time"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_gravity="left"
tools:text="30 Nov 18"
/>
</LinearLayout>
如果在派生视图中执行此操作并覆盖 onMeasure(),则计算不会直接进行,并且 A、C 宽度可能不会立即可用。
如何只在布局xml中做到这一点,或者有可能吗?
【问题讨论】: