我尝试并排放置三个视图:text1 - img - text2。 Img必须始终在text1上(没有额外的空间)。我试图用布局权重来解决这个问题,但是通过这个解决方案,img始终处于父视图的中心。并排视图,动态尺寸
预期结果:
文本1短,文本2长。不要文本1和IMG之间添加空间
+----------------------------------------+
| [Text1 a] [img] [Text2 asdfghjklz... ] |
+----------------------------------------+
ellipsized文字,保持IMG中心
+----------------------------------------+
| [Text1 asdfg...] [img] [Text2 asdf...] |
+----------------------------------------+
文本1长,文本2短
+----------------------------------------+
| [Text1 asdfgadfafs...] [img] [Text2 a] |
+----------------------------------------+
这是最接近我可以得到(使用重量),但问题是,如果text1短,img仍然在布局的中心。我应该总是直接在text1的右侧。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
tools:text="Text1 asdfghjkl" />
<ImageView
android:id="@+id/img"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:src="@drawable/ic_arrow" />
<TextView
android:id="@+id/textview2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
tools:text="Text2 asdfghjkl" />
</LinearLayout>
那么如果Text1 long,img&Text2短是什么要求? – sJy
新增了一个例子,所以尽可能地将text2推向右边。 – devha