外层父线性布局宽度有限,三个 child :
[child A][<- child b ->][child c]
对于 child b,它有两个 child
[<- child b ->]
[element 1][element 2]
希望 child 2 流过 child 1,如果没有足够的空间,那么 child 1 应该显示省略号
[<- child b ->]
[element 1 xxx...][element 2]
尝试了以下布局,当 child 1 太长时,它会占用所有空间并将 child 2 推出视野。
[<- child b ->]
[element 1 xxxxxxxxxxxxxxxx...]
如果指定 child 1有
android:layout_width="0dp"
android:layout_weight="1"
如果 child 1 的内容较短,则 child 2 不会流过它,而是 child 2 对齐到右侧。
[<- child b ->]
[element 1] [element 2]
并且不想为 child 1 提供硬编码的 maxWidth。 有什么建议吗?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight=“3dp”
android:ellipsize="end"
android:singleLine="true"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"/>
</LinearLayout>
最佳答案
我能想到的最接近的事情,如果不是创建自定义 ViewGroup 或更改代码中的宽度。
使用 wrap_content 设置两者并将第一个 TextView 的 maxWidth
设置为安全数字,这样它就不会推开第二个 TextView。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="50dp"
android:layout_marginRight=“3dp”
android:ellipsize="end"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
关于android - 如何让右侧子元素在水平布局的左侧子元素旁边流动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32359224/