我有一个列表视图,每个项目是一个相关的布局,其中有几个文字浏览在一行,但如果其中一个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);
}
}
}
}
我建议你为'TextView'使用'LinearLayout',并考虑一个'TextView'的最大宽度(阈值)。 –
首先编辑问题以更具体:添加有问题的布局。 – laalto