Android中当一行显示两个TextView时,第一个可“…”,而第二个必须显示全部

来源:互联网 发布:mac屏保设置不生效 编辑:程序博客网 时间:2024/06/05 09:32

在Android中当一行显示两个TextView时,第一个可“…”,而第二个必须全部显示这种布局时:

<RelativeLayout    android:layout_width = "wrap_content"    android:layout_height = "wrap_content"    android:background="#cccccc"    android:padding="20dip">    <LinearLayout        android:layout_width = "wrap_content"        android:layout_height = "wrap_content"        android:orientation = "horizontal" >        <TextView            android:id = "@+id/item_text_one"            android:layout_width = "wrap_content"            android:layout_height = "wrap_content"            android:layout_weight = "1"            android:ellipsize = "end"            android:singleLine = "true"            android:text = "text1的效果 效果 效果 效果 效果 效果 效果"  />        <TextView            android:id = "@+id/item_text_two"            android:layout_width = "wrap_content"            android:layout_height = "wrap_content"            android:layout_marginLeft = "20dip"            android:text = "text2的效果"  />    </LinearLayout></RelativeLayout>

运行结果为:

          


看上面代码可知,text1使用了weight属性,并置为1,text2没有使用,默认weight为0。

其实,layout_weight属性的意思就是说:android系统会按照两个TextView的warp_content来分配宽度,剩下的因为只有text1的weight = 1,所以剩下的空间都留给了text1,就能达成这种效果。


1 0
原创粉丝点击