2016-08-18 35 views
0

我尝试并排放置三个视图: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> 
+0

那么如果Text1 long,img&Text2短是什么要求? – sJy

+0

新增了一个例子,所以尽可能地将text2推向右边。 – devha

回答

0

使用相对布局将每个小部件对齐到其他小部件的右侧。对于第一个的TextView给予一定宽度和添加低于2线

机器人:ellipsize =“结束” 机器人:MAXLINES =“1”

为图像使用您需要的高度和宽度

用于第三textview使用下面的代码。对于这个textview你不必设置你可以使用换行内容的宽度。

机器人:ellipsize = “结束” 机器人:MAXLINES = “1”

希望这有助于或至少这将是一个良好的开端。

+0

我想第一个文本字段根据文本长度调整其宽度,所以我不能给它任何固定的宽度。另外,如果我使用相对布局并对齐其他视图,text1如果足够长,则将text2完全推出视图。 – devha