ViewAnimator
2 つの子ビューを含む があります。ViewAnimator
現在アクティブな子に関係なく、最初の子のサイズにしたいと思います。これは少しの設定で可能ですか、それとも独自のサブクラスとオーバーライドonMeasure
などを実装する必要がありますか?
質問する
318 次
1 に答える
1
私はこれを拡張し、レイアウトの寸法を最初の子の寸法にViewAnimator
オーバーライドし、設定することで解決しました。onMeasure
これが私が思いついたものです。FrameLayout.onMeasure
それは基本的にメソッドの小さな変更です。
/**
* Overrides the default {@link android.widget.FrameLayout#onMeasure(int, int)}
* behavior to allow a main child to be defined which determines the size of the
* layout. By default, the max size of all children is used as the layout size.
*/
public class SizePinnedViewAnimator extends ViewAnimator {
private final int mainChildIndex;
@SuppressWarnings("UnusedDeclaration")
public SizePinnedViewAnimator(final Context context) {
super(context);
mainChildIndex = 0;
}
@SuppressWarnings("UnusedDeclaration")
public SizePinnedViewAnimator(final Context context, final AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(
attrs,
R.styleable.SizePinnedViewAnimator);
mainChildIndex = a.getInt(R.styleable.SizePinnedViewAnimator_mainChild, 0);
a.recycle();
}
/**
* {@inheritDoc}
*
* Copied from {@link android.widget.FrameLayout#onMeasure(int, int)}
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int count = getChildCount();
View mainChild = getChildAt(mainChildIndex);
if(mainChild == null) {
throw new IllegalStateException("No child at index " + mainChildIndex);
}
measureChildWithMargins(mainChild, widthMeasureSpec, 0, heightMeasureSpec, 0);
int maxHeight = mainChild.getMeasuredHeight();
int maxWidth = mainChild.getMeasuredWidth();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
// Find rightmost and bottommost child
for (int i = 0; i < count; i++) {
if(i == mainChildIndex) continue;
final View child = getChildAt(i);
if (getConsiderGoneChildrenWhenMeasuring() || child.getVisibility() != GONE) {
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
}
// Don't have access to the foreground padding numbers, but we're not
// using a foreground drawable anyway, so ignore it.
// Account for padding too
maxWidth += getPaddingLeft() + getPaddingRight();
maxHeight += getPaddingTop() + getPaddingBottom();
// Check against our minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
resolveSize(maxHeight, heightMeasureSpec));
}
}
で定義された属性res/values/attrs.xml
:
<declare-styleable name="SizePinnedViewAnimator">
<attr name="mainChild" format="integer" />
</declare-styleable>
于 2012-08-22T13:59:12.917 に答える