サイズが幅: 100dp、高さ: 100dp の垂直方向の単純な LinearLayout があるとします。
レイアウト内には 10 個の TextView があります (幅: fill_parent、高さ: wrap_content、max_lines = 1、scroll_horizontally = 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);
}
}
}
}`