Java 如何在LinearLayout内部使部分可见的视图不可见?

Java 如何在LinearLayout内部使部分可见的视图不可见?,java,android,layout,views,Java,Android,Layout,Views,假设我们有一个垂直方向的简单线性布局,尺寸宽度:100dp,高度:100dp 布局内部有10个文本视图(宽度:填充父视图,高度:包裹内容,最大行数=1,水平滚动=真,椭圆化=结束)。每个文本视图都可见,并填充14dp文本“What a text”。android设备的最终密度并不重要。大多数文本视图将正确显示,但由于强制的布局大小,其中一些将不可见或被剪裁 目标是:检测剪裁视图并隐藏它们 我尝试使用自定义LinearLayout子类,在布局阶段,每个子视图都被测量并与目标大小进行比较。问题在于,

假设我们有一个垂直方向的简单线性布局,尺寸宽度:100dp,高度:100dp

布局内部有10个文本视图(宽度:填充父视图,高度:包裹内容,最大行数=1,水平滚动=真,椭圆化=结束)。每个文本视图都可见,并填充14dp文本“What a text”。android设备的最终密度并不重要。大多数文本视图将正确显示,但由于强制的布局大小,其中一些将不可见或被剪裁

目标是:检测剪裁视图并隐藏它们

我尝试使用自定义LinearLayout子类,在布局阶段,每个子视图都被测量并与目标大小进行比较。问题在于,measure调用会更改内部视图度量值—如果子视图不是简单视图而是视图组—则不会正确显示。据我所知,在测量阶段之后,应该有布局阶段。但一切都发生在定制线性布局的布局阶段

编辑:

好的,简化我的问题-我想要一个线性布局或一般来说-一个视图组,它不会绘制部分可见的子对象

自定义布局类的代码:

public final class ClipAwareLinearLayout extends LinearLayout
{    
    public ClipAwareLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ClipAwareLinearLayout(Context context)
    {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        final int width = r - l;
        final int height = b - t;
        final int count = getChildCount();
        final int msWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST);
        final int msHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
        View child;
        int measuredHeight;
        int childHeight;
        for (int i = 0; i < count; ++i)
        {
            child = getChildAt(i);
            if (child != null)
            {
                childHeight = child.getHeight();
                child.measure(msWidth, msHeight);
                measuredHeight = child.getMeasuredHeight();
                final boolean clipped = (childHeight < measuredHeight);
                child.setVisibility(clipped ? View.INVISIBLE : View.VISIBLE);
            }
        }
    }

}`
公共最终类ClipAwareLinearLayout扩展了LinearLayout
{    
公共ClipWareLinearLayout(上下文、属性集属性)
{
超级(上下文,attrs);
}
公共ClipAwareLinearLayout(上下文)
{
超级(上下文);
}
@凌驾
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b)
{
超级在线布局(更改,l,t,r,b);
最终整数宽度=r-l;
最终内部高度=b-t;
最终整数计数=getChildCount();
final int msWidth=MeasureSpec.makeMeasureSpec(最大宽度,MeasureSpec.AT_);
final int msHeight=测量等级makeMeasureSpec(高度,最大测量等级);
看孩子;
内测高度;
儿童身高;
对于(int i=0;i
尝试下面的代码。它应该可以工作,但我还没有测试,所以我可能是错的:

class ClippedLinear extends LinearLayout {

    public ClippedLinear(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        boolean status = false;
        for (int i = getChildCount() - 1; i > 0; i--) {
            if (status) {
                continue;
            }
            final View child = getChildAt(i);
            final int childHeight = child.getMeasuredHeight();
            if (childHeight == 0) {
                child.setVisibility(View.GONE);         
            } else {                
                child.measure(widthMeasureSpec, heightMeasureSpec);
                if (childHeight < child.getMeasuredHeight()) {                  
                    child.setVisibility(View.GONE);
                }
                status = true;
            }
        }
    }

}
类ClippedLinear扩展了LinearLayout{
公共剪裁线性(上下文、属性集属性){
超级(上下文,attrs);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
布尔状态=假;
对于(int i=getChildCount()-1;i>0;i--){
如果(状态){
继续;
}
最终视图子对象=getChildAt(i);
final int childHeight=child.getMeasuredHeight();
if(childHeight==0){
setVisibility(View.GONE);
}否则{
儿童测量(宽度测量,高度测量);
如果(childHeight
您到底想做什么?抽象你的问题,可能有一些更简单的方法来实现它。.你说的“剪裁”是指
textview
文本宽度不合适吗?基本上,您希望在父级
LinearLayout
的强制维度中显示尽可能多的
textview
,而不剪裁它们,不是吗?也许我不太理解,但是,如果问题是如何理解哪些textview是可见的,您可以计算显示高度和,当您添加新的textView时,您可以根据textView高度计算其中哪些是可见的或不可见的。例如,显示高度为100?文本视图高度是10?毫无疑问,10个文本视图将可见。我重复一遍,我不知道我是否理解你的问题。@Luksprog-是的,这就是我想要实现的目标。目前只有垂直方向起作用。布局的子视图将始终采用填充父模式_width@MichaelP你能解决这个问题吗?我想知道这个问题的答案。