2

サイズが幅: 100dp、高さ: 100dp の垂直方向の単純な LinearLayout があるとします。

レイアウト内には 10 個の TextView があります (幅: fill_parent、高さ: wrap_content、max_lines = 1、scroll_horizo​​ntally = true、ellipsize = end)。各テキスト ビューが表示され、14 dp のテキスト「What a text」が表示されます。Android デバイスの最終的な密度は問題ではありません。ほとんどの TextView は正しく表示されますが、レイアウト サイズが強制されているため、一部が見えなくなったり、クリップされたりします。

目標は、クリップされたビューを検出し、それらを非表示にすることです。

カスタム LinearLayout サブクラスを使用しようとしました。レイアウト段階で、各子ビューが測定され、目的のサイズと比較されました。問題は、メジャー コールが内部ビューの測定値を変更することです。子が単純なビューではなく ViewGroup の場合、正しく表示されません。私の知る限り、測定フェーズの後、レイアウト フェーズが必要です。ただし、すべてはカスタム LinearLayout のレイアウト フェーズ内ですでに行われています。

編集:

OK、私の質問を単純化します - 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);
            }
        }
    }

}`
4

1 に答える 1

0

以下のコードを試してください。動作するはずですが、テストしていないので間違っている可能性があります。

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

}
于 2012-11-21T16:29:56.870 に答える