【问题标题】:How to make invisible a partially visible view inside LinearLayout?如何使 LinearLayout 中的部分可见视图不可见?
【发布时间】:2012-11-09 19:38:17
【问题描述】:

假设我们有一个简单的 LinearLayout,它具有垂直方向,大小宽度:100dp,高度:100dp

布局内部有 10 个 TextView(宽度:fill_parent,高度:wrap_content,max_lines = 1,scroll_horizo​​ntally = true,ellipsize = end)。每个文本视图都是可见的,并填充有 14dp 文本“What a text”。 android 设备的最终密度无关紧要。大多数 TextView 将正确显示,但由于强制布局大小,其中一些将不可见或被裁剪。

目标是:检测剪辑的视图并隐藏它们。

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

编辑:

好的,简化我的问题 - 我想要一个 LinearLayout 或一般来说 - 一个 ViewGroup,它不会绘制部分可见的孩子。

自定义布局类代码:

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);
            }
        }
    }

}`

【问题讨论】:

  • 你到底想做什么?抽象你的问题,可能有一些更简单的方法来实现它..
  • 剪切是否还意味着TextViews 文本不适合宽度?您基本上希望在父级LinearLayout 的强加尺寸中尽可能多地显示TextViews,而不剪裁它们,不是吗?
  • 也许我不太理解,但是,如果问题是如何理解哪些 textview 可见或不可见,您可以计算显示高度,并且当您添加新的 textView 时,您可以计算,根据到 textview 高度,其中哪些将可见或不可见。例如,显示高度为 100 ? TextView 高度是 10 吗?毫无疑问,10 个 TextView 将是可见的。我再说一遍,我不知道我是否理解你的问题。
  • @Luksprog - 是的,这就是我想要实现的。目前只有垂直方向很重要。子视图对于 layout_width 将始终具有 fill_parent 模式
  • @MichaelP 你能解决这个问题吗?我想回答这个问题。

标签: java android layout views


【解决方案1】:

试试下面的代码。它应该可以工作,但我没有测试过,所以我可能错了:

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;
            }
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多