2014-01-15 22 views
0

我有一个列表视图,每个项目是一个相关的布局,其中有几个文字浏览在一行,但如果其中一个textview的文本太长,这将导致它后面的textview是所以我想让它消失。但是我无法知道listview中的每个项目的宽度,并且无法知道relativelayout中的每个textview,我看到android api支持度量方法,但效率很低,因为如果我测量每个项目小工具,系统将在我之后做同样的事情。请提供一个很好的方法来解决我的问题,谢谢。Android的RelativeLayout导致它的孩子互相掩盖

@Muhannad A.Alhariri这是我的解决方案:

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
super.onLayout(changed, l, t, r, b); 
int sumChildWidth = 0; 
for (int i = 0; i < getChildCount(); i++) { 
    View child = getChildAt(i); 
    if (child != null) { 
    if (child.getVisibility() != View.GONE 
     && child.getVisibility() != View.INVISIBLE) { 
     sumChildWidth += child.getWidth(); 
    } 
    } 
} 
int myWidth = Math.round(getWidth() 
    - getContext().getResources().getDisplayMetrics().density * 15); 

if (sumChildWidth > myWidth) { 
    if (getChildCount() > 0 && getChildCount() - 1 >= 0) { 
    View lastChild = getChildAt(getChildCount() - 1); 
    if (lastChild != null) { 
     lastChild.layout(0, 0, 0, 0); 
    } 
    } 
} 
} 
+0

我建议你为'TextView'使用'LinearLayout',并考虑一个'TextView'的最大宽度(阈值)。 –

+0

首先编辑问题以更具体:添加有问题的布局。 – laalto

回答

0

为什么不使用RelativeLayout的性能,如

toRightOf and toLeftOf 
+0

但长文本背后的文字浏览会被挤出相关布局 – KrystalJake

+0

@KrystalJake为什么不使用maxWidth和maxHeight?或者你可以使用LinearLayout与重力而不是RelativeLayout –

+0

我发现我必须重写RelativeLayout,然后我判断它的子节点的总宽度,如果总宽度大于RelativeLayout的宽度,我解雇它的一些子节点 – KrystalJake

0

我想这个古怪的TextView的长度过长,因为你不是使用省略号。在TextView中使用省略号

机器人:ellipsize =“结束”

这是做没有布局将解决您的问题的正确方法。

相关问题